Commit 9518e3948b
Verified · cmc
Layout: unified · split
Sources/OrgApp/DocumentSession.swift +8 −4
| @@ -63,6 +63,10 @@ public final class DocumentSession { | ||
| 63 | 63 | public var lastError: String? |
| 64 | 64 | public var saveMode: SaveMode = .automatic |
| 65 | 65 | public var autosaveDelay: Duration = .seconds(1) |
| 66 | /// Org options for commands the session runs (refile, archive). | |
| 67 | public var options: EditingOptions = .org | |
| 68 | /// TODO keywords and priorities for files without their own settings. | |
| 69 | public var defaults: OrgSettings = .default | |
| 66 | 70 | |
| 67 | 71 | private let fileSystem: FileSystem |
| 68 | 72 | private let recovery: RecoveryStore |
| @@ -90,7 +94,7 @@ public final class DocumentSession { | ||
| 90 | 94 | } |
| 91 | 95 | autosave?.cancel() |
| 92 | 96 | self.path = path |
| 93 | buffer = StateBuffer(DocumentState(bytes: bytes)) | |
| 97 | buffer = StateBuffer(DocumentState(bytes: bytes, defaults: defaults)) | |
| 94 | 98 | conflict = nil |
| 95 | 99 | revealRequest = nil |
| 96 | 100 | refresh() |
| @@ -197,7 +201,7 @@ public final class DocumentSession { | ||
| 197 | 201 | private func editContext(now: Date) -> EditContext? { |
| 198 | 202 | guard let buffer else { return nil } |
| 199 | 203 | let document = buffer.document |
| 200 | return EditContext(revision: document.revision, text: document.text, tree: document.tree, selection: buffer.selection, now: now) | |
| 204 | return EditContext(revision: document.revision, text: document.text, tree: document.tree, selection: buffer.selection, now: now, options: options) | |
| 201 | 205 | } |
| 202 | 206 | |
| 203 | 207 | /// Moves the subtree at the caret under `target`. For another file, that file is written |
| @@ -215,7 +219,7 @@ public final class DocumentSession { | ||
| 215 | 219 | let url = URL(fileURLWithPath: target.path) |
| 216 | 220 | do { |
| 217 | 221 | guard let bytes = try fileSystem.read(url) else { return "\((target.path as NSString).lastPathComponent) no longer exists." } |
| 218 | var state = DocumentState(bytes: bytes) | |
| 222 | var state = DocumentState(bytes: bytes, defaults: defaults) | |
| 219 | 223 | guard state.isEditable else { return "\((target.path as NSString).lastPathComponent) isn't UTF-8, so it can't be changed." } |
| 220 | 224 | guard let start = target.locate(in: state) else { return "Can't find \(target.label) any more." } |
| 221 | 225 | guard let result = Refile.toOtherFile(source: context, target: state.text, under: start) else { return "Can't refile from here." } |
| @@ -241,7 +245,7 @@ public final class DocumentSession { | ||
| 241 | 245 | do { |
| 242 | 246 | let url = URL(fileURLWithPath: destination.path) |
| 243 | 247 | let existing = destination.isSameFile ? nil : try fileSystem.read(url) |
| 244 | var state = DocumentState(bytes: existing ?? []) | |
| 248 | var state = DocumentState(bytes: existing ?? [], defaults: defaults) | |
| 245 | 249 | if !destination.isSameFile, !state.isEditable { return "\(url.lastPathComponent) isn't UTF-8, so it can't be changed." } |
| 246 | 250 | let edits: Archive.Edits |
| 247 | 251 | switch Archive.run(context, sourcePath: path, destination: destination, archiveText: existing.map { _ in state.text }) { |
Sources/OrgCore/Commands/Archive.swift +1 −1
| @@ -234,7 +234,7 @@ public enum Archive { | ||
| 234 | 234 | formatter.timeZone = context.calendar.timeZone |
| 235 | 235 | formatter.dateFormat = "yyyy-MM-dd EEE HH:mm" |
| 236 | 236 | let time = formatter.string(from: context.now) |
| 237 | let archive = destination.isSameFile ? nil : EmacsBuffer(archiveText ?? "", point: 0, settings: context.tree.settings) | |
| 237 | let archive = destination.isSameFile ? nil : EmacsBuffer(archiveText ?? "", point: 0, settings: context.tree.settings, options: context.options) | |
| 238 | 238 | var archiveEdit: TextEdit? |
| 239 | 239 | let step = commitBuffer(context) { source in |
| 240 | 240 | let target = archive ?? source |
Sources/OrgCore/Commands/Command.swift +27 −1
| @@ -1,5 +1,28 @@ | ||
| 1 | 1 | import Foundation |
| 2 | 2 | |
| 3 | /// Org options that change what commands write, as the user's Emacs has them. | |
| 4 | public struct EditingOptions: Sendable, Equatable, Codable { | |
| 5 | /// `org-tags-column`: negative aligns tags to end at that column, 0 puts them one space | |
| 6 | /// after the title. | |
| 7 | public var tagsColumn: Int | |
| 8 | /// `org-insert-heading-respect-content`: M-RET adds the heading after the subtree. | |
| 9 | public var insertHeadingRespectContent: Bool | |
| 10 | /// `org-M-RET-may-split-line`: M-RET in the middle of a line splits it. | |
| 11 | public var metaReturnMaySplitLine: Bool | |
| 12 | /// `org-list-allow-alphabetical`: `a.` and `A)` are list bullets. | |
| 13 | public var listAllowAlphabetical: Bool | |
| 14 | ||
| 15 | public init(tagsColumn: Int = -77, insertHeadingRespectContent: Bool = false, metaReturnMaySplitLine: Bool = true, listAllowAlphabetical: Bool = false) { | |
| 16 | self.tagsColumn = tagsColumn | |
| 17 | self.insertHeadingRespectContent = insertHeadingRespectContent | |
| 18 | self.metaReturnMaySplitLine = metaReturnMaySplitLine | |
| 19 | self.listAllowAlphabetical = listAllowAlphabetical | |
| 20 | } | |
| 21 | ||
| 22 | /// Org's own defaults. | |
| 23 | public static let org = EditingOptions() | |
| 24 | } | |
| 25 | ||
| 3 | 26 | /// Everything a command may read. Commands never read the clock or the file system, so the |
| 4 | 27 | /// same context always gives the same result. |
| 5 | 28 | public struct EditContext: Sendable { |
| @@ -16,10 +39,12 @@ public struct EditContext: Sendable { | ||
| 16 | 39 | public let answers: [String: String] |
| 17 | 40 | /// Ranges the view hides (folded text); motion commands skip headings inside them. |
| 18 | 41 | public let hidden: [Range<Int>] |
| 42 | public let options: EditingOptions | |
| 19 | 43 | |
| 20 | 44 | public init( |
| 21 | 45 | revision: Int, text: String, tree: OrgTree, selection: [Range<Int>], |
| 22 | now: Date = Date(), calendar: Calendar = .current, answers: [String: String] = [:], hidden: [Range<Int>] = [] | |
| 46 | now: Date = Date(), calendar: Calendar = .current, answers: [String: String] = [:], hidden: [Range<Int>] = [], | |
| 47 | options: EditingOptions = .org | |
| 23 | 48 | ) { |
| 24 | 49 | self.revision = revision |
| 25 | 50 | self.text = text |
| @@ -29,6 +54,7 @@ public struct EditContext: Sendable { | ||
| 29 | 54 | self.calendar = calendar |
| 30 | 55 | self.answers = answers |
| 31 | 56 | self.hidden = hidden |
| 57 | self.options = options | |
| 32 | 58 | } |
| 33 | 59 | |
| 34 | 60 | public var caret: Int { selection.first?.lowerBound ?? 0 } |
Sources/OrgCore/Commands/EmacsBuffer.swift +7 −4
| @@ -28,10 +28,13 @@ final class EmacsBuffer { | ||
| 28 | 28 | /// Groups of the last successful match, as Emacs's match data. |
| 29 | 29 | var lastMatch: [Range<Int>?] = [] |
| 30 | 30 | |
| 31 | init(_ text: String, point: Int, settings: OrgSettings = .default) { | |
| 31 | let options: EditingOptions | |
| 32 | ||
| 33 | init(_ text: String, point: Int, settings: OrgSettings = .default, options: EditingOptions = .org) { | |
| 32 | 34 | self.text = NSMutableString(string: text) |
| 33 | 35 | self.point = point |
| 34 | 36 | self.settings = settings |
| 37 | self.options = options | |
| 35 | 38 | unchangedPrefix = self.text.length |
| 36 | 39 | unchangedSuffix = self.text.length |
| 37 | 40 | } |
| @@ -407,14 +410,14 @@ final class EmacsBuffer { | ||
| 407 | 410 | let end = lineEnd(offset) |
| 408 | 411 | let line = substring(start, end) |
| 409 | 412 | var plain = LineBuffer(line, caret: nil) |
| 410 | OrgCore.alignTags(&plain) | |
| 413 | OrgCore.alignTags(&plain, tagsColumn: options.tagsColumn) | |
| 411 | 414 | guard plain.string != line else { return } |
| 412 | 415 | func map(_ position: Int) -> Int { |
| 413 | 416 | guard position >= start, position <= end else { |
| 414 | 417 | return position > end ? position + (plain.string as NSString).length - (line as NSString).length : position |
| 415 | 418 | } |
| 416 | 419 | var buffer = LineBuffer(line, caret: position - start) |
| 417 | OrgCore.alignTags(&buffer) | |
| 420 | OrgCore.alignTags(&buffer, tagsColumn: options.tagsColumn) | |
| 418 | 421 | return start + buffer.caret! |
| 419 | 422 | } |
| 420 | 423 | let newPoint = map(point) |
| @@ -430,7 +433,7 @@ final class EmacsBuffer { | ||
| 430 | 433 | /// Runs a buffer-editing body against a context and turns the result into one minimal edit |
| 431 | 434 | /// and the new caret. A user error becomes `.failed`. |
| 432 | 435 | func commitBuffer(_ context: EditContext, _ body: (EmacsBuffer) throws -> Void) -> CommandStep { |
| 433 | let buffer = EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings) | |
| 436 | let buffer = EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings, options: context.options) | |
| 434 | 437 | do { |
| 435 | 438 | try body(buffer) |
| 436 | 439 | } catch let error as EmacsBuffer.UserError { |
Sources/OrgCore/Commands/HeadingCommands.swift +4 −4
| @@ -247,7 +247,7 @@ public struct TodoCycle: OrgCommand { | ||
| 247 | 247 | } |
| 248 | 248 | } |
| 249 | 249 | buffer.replaceBeforeMarkers(regionStart..<regionEnd, with: next.map { " \($0) " } ?? " ") |
| 250 | alignTags(&buffer) | |
| 250 | alignTags(&buffer, tagsColumn: context.options.tagsColumn) | |
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | 253 | } |
| @@ -295,7 +295,7 @@ struct PriorityChange { | ||
| 295 | 295 | buffer.insert(cookie + " ", at: line.titleStart - line.start) |
| 296 | 296 | } |
| 297 | 297 | } |
| 298 | alignTags(&buffer) | |
| 298 | alignTags(&buffer, tagsColumn: context.options.tagsColumn) | |
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | 301 | |
| @@ -341,7 +341,7 @@ public struct PromoteHeading: OrgCommand { | ||
| 341 | 341 | guard line.level > 1 else { return .failed("Cannot promote to level 0") } |
| 342 | 342 | return line.commit(context) { buffer in |
| 343 | 343 | buffer.replace(0..<(line.level + 1), with: String(repeating: "*", count: line.level - 1) + " ") |
| 344 | alignTags(&buffer) | |
| 344 | alignTags(&buffer, tagsColumn: context.options.tagsColumn) | |
| 345 | 345 | fixPositionAfterPromote(&buffer, settings: context.tree.settings) |
| 346 | 346 | } |
| 347 | 347 | } |
| @@ -358,7 +358,7 @@ public struct DemoteHeading: OrgCommand { | ||
| 358 | 358 | let line = HeadingLine(heading) |
| 359 | 359 | return line.commit(context) { buffer in |
| 360 | 360 | buffer.replace(0..<(line.level + 1), with: String(repeating: "*", count: line.level + 1) + " ") |
| 361 | alignTags(&buffer) | |
| 361 | alignTags(&buffer, tagsColumn: context.options.tagsColumn) | |
| 362 | 362 | fixPositionAfterPromote(&buffer, settings: context.tree.settings) |
| 363 | 363 | } |
| 364 | 364 | } |
Sources/OrgCore/Commands/ListCommands.swift +5 −5
| @@ -11,7 +11,7 @@ extension EmacsBuffer { | ||
| 11 | 11 | var structure = saveExcursion { point = start; return listStruct() } |
| 12 | 12 | let prevs = structure.prevs |
| 13 | 13 | let description = structure.listType(start, prevs) == .descriptive ? " :: " : nil |
| 14 | structure = insertItem(at: pos, structure, prevs, checkbox: checkbox, afterBullet: description) | |
| 14 | structure = insertItem(at: pos, structure, prevs, checkbox: checkbox, afterBullet: description, splitLine: options.metaReturnMaySplitLine) | |
| 15 | 15 | writeStruct(&structure, structure.parents) |
| 16 | 16 | if checkbox { updateCheckboxCount() } |
| 17 | 17 | forwardLine(0) |
| @@ -91,17 +91,17 @@ extension EmacsBuffer { | ||
| 91 | 91 | let upper = marker(lineEnd(point)) |
| 92 | 92 | defer { release(upper) } |
| 93 | 93 | point = lower |
| 94 | let current = looking(Self.itemPattern).flatMap { match -> String? in | |
| 94 | let current = looking(itemPattern).flatMap { match -> String? in | |
| 95 | 95 | let box = saveExcursion { () -> [Range<Int>?]? in |
| 96 | 96 | point = match[0]!.upperBound |
| 97 | if let counter = looking("(?:\\[@(?:start:)?[0-9]+\\][ \\t]*)") { point = counter[0]!.upperBound } | |
| 97 | if let counter = looking(options.listAllowAlphabetical ? "(?:\\[@(?:start:)?([0-9]+|[A-Za-z])\\][ \\t]*)" : "(?:\\[@(?:start:)?[0-9]+\\][ \\t]*)") { point = counter[0]!.upperBound } | |
| 98 | 98 | return looking("(\\[[- X]\\])[ \\t]+") |
| 99 | 99 | } |
| 100 | 100 | return box.map { substring($0[1]!.lowerBound, $0[1]!.upperBound) } |
| 101 | 101 | } |
| 102 | 102 | let reference = current == "[X]" ? "[ ]" : "[X]" |
| 103 | 103 | point = lower |
| 104 | while point < upper.position, searchForward(Self.itemBeginningPattern, bound: upper.position, move: true) != nil { | |
| 104 | while point < upper.position, searchForward(itemBeginningPattern, bound: upper.position, move: true) != nil { | |
| 105 | 105 | var structure = listStruct() |
| 106 | 106 | let old = structure |
| 107 | 107 | let parents = structure.parents |
| @@ -302,7 +302,7 @@ public struct InsertCheckboxItem: OrgCommand { | ||
| 302 | 302 | |
| 303 | 303 | /// An item command that needs point on an item's first line. |
| 304 | 304 | private func atItemLine(_ context: EditContext) -> Bool { |
| 305 | EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings).atItem | |
| 305 | EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings, options: context.options).atItem | |
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /// M-right on an item: `org-indent-item`. |
Sources/OrgCore/Commands/OrgList.swift +57 −24
| @@ -247,8 +247,13 @@ extension EmacsBuffer { | ||
| 247 | 247 | |
| 248 | 248 | // MARK: - Context |
| 249 | 249 | |
| 250 | static let itemPattern = "([ \\t]*([-+]|(([0-9]+)[.)]))|[ \\t]+\\*)([ \\t]+|$)" | |
| 251 | static let itemBeginningPattern = "^" + itemPattern | |
| 250 | /// `org-item-re`. | |
| 251 | var itemPattern: String { | |
| 252 | let alpha = options.listAllowAlphabetical ? "|[A-Za-z]" : "" | |
| 253 | return "([ \\t]*([-+]|(([0-9]+\(alpha))[.)]))|[ \\t]+\\*)([ \\t]+|$)" | |
| 254 | } | |
| 255 | ||
| 256 | var itemBeginningPattern: String { "^" + itemPattern } | |
| 252 | 257 | static let fullItemPattern = "^[ \\t]*((?:[-+*]|(?:[0-9]+|[A-Za-z])[.)])(?:[ \\t]+|$))(?:\\[@(?:start:)?([0-9]+|[A-Za-z])\\][ \\t]*)?(?:(\\[[ X-]\\])(?:[ \\t]+|$))?(?:(.*)[ \\t]+::(?:[ \\t]+|$))?" |
| 253 | 258 | static let listEndPattern = "^[ \\t]*\\n[ \\t]*\\n" |
| 254 | 259 | static let drawerPattern = "^[ \\t]*:([-_\\w]+):[ \\t]*$" |
| @@ -307,7 +312,7 @@ extension EmacsBuffer { | ||
| 307 | 312 | let context = listContext() |
| 308 | 313 | var reference = looking("^[ \\t]*$") != nil ? 10000 : currentTextIndentation |
| 309 | 314 | if context.kind == .invalid { return nil } |
| 310 | if looking(Self.itemPattern) != nil { return point } | |
| 315 | if looking(itemPattern) != nil { return point } | |
| 311 | 316 | // Inside `org-list-end-re`: start above it. |
| 312 | 317 | let windowStart = saveExcursion { forwardLine(-2); return point } |
| 313 | 318 | let windowEnd = saveExcursion { forwardLine(2); return lineEnd(point) } |
| @@ -322,7 +327,7 @@ extension EmacsBuffer { | ||
| 322 | 327 | } |
| 323 | 328 | while true { |
| 324 | 329 | let ind = currentTextIndentation |
| 325 | if looking(Self.itemPattern) != nil, ind < reference { return point } | |
| 330 | if looking(itemPattern) != nil, ind < reference { return point } | |
| 326 | 331 | if point <= context.up { return nil } |
| 327 | 332 | if looking(Self.listEndPattern) != nil { return nil } |
| 328 | 333 | if looking("^[ \\t]*#\\+end_") != nil, searchBackward("^[ \\t]*#\\+begin_", bound: context.up) != nil { |
| @@ -350,7 +355,7 @@ extension EmacsBuffer { | ||
| 350 | 355 | beginningOfLine() |
| 351 | 356 | switch listContext().kind { |
| 352 | 357 | case .invalid, .block("comment"), .block("verse"): return false |
| 353 | default: return looking(Self.itemPattern) != nil | |
| 358 | default: return looking(itemPattern) != nil | |
| 354 | 359 | } |
| 355 | 360 | } |
| 356 | 361 | } |
| @@ -385,7 +390,7 @@ extension EmacsBuffer { | ||
| 385 | 390 | while true { |
| 386 | 391 | let ind = currentTextIndentation |
| 387 | 392 | if point <= context.up { |
| 388 | if looking(Self.itemPattern) == nil { | |
| 393 | if looking(itemPattern) == nil { | |
| 389 | 394 | trimToBegin() |
| 390 | 395 | } else { |
| 391 | 396 | beginCell = (point, ind) |
| @@ -397,7 +402,7 @@ extension EmacsBuffer { | ||
| 397 | 402 | trimToBegin() |
| 398 | 403 | return |
| 399 | 404 | } |
| 400 | if looking(Self.itemPattern) != nil { | |
| 405 | if looking(itemPattern) != nil { | |
| 401 | 406 | items.insert(assocAtPoint(ind), at: 0) |
| 402 | 407 | ends.insert((ind, point), at: 0) |
| 403 | 408 | if ind < textMinInd { beginCell = (point, ind) } |
| @@ -429,7 +434,7 @@ extension EmacsBuffer { | ||
| 429 | 434 | ends2.append((0, point)) |
| 430 | 435 | break |
| 431 | 436 | } |
| 432 | if looking(Self.itemPattern) != nil { | |
| 437 | if looking(itemPattern) != nil { | |
| 433 | 438 | items2.append(assocAtPoint(ind)) |
| 434 | 439 | ends2.append((ind, point)) |
| 435 | 440 | forwardLine(1) |
| @@ -747,27 +752,38 @@ extension EmacsBuffer { | ||
| 747 | 752 | |
| 748 | 753 | /// `org-list-struct-fix-bul` (alphabetical bullets off). |
| 749 | 754 | func fixBullets(_ structure: inout ListStruct, _ prevs: [Int: Int]) { |
| 755 | func find(_ pattern: String, _ string: String) -> Range<String.Index>? { | |
| 756 | guard let range = string.range(of: pattern, options: .regularExpression) else { return nil } | |
| 757 | stringMatched(string, range) | |
| 758 | return range | |
| 759 | } | |
| 750 | 760 | for pos in structure.map(\.pos) { |
| 751 | 761 | let previous = prevs[pos] |
| 752 | 762 | let counter = structure.item(pos)!.counter |
| 753 | 763 | let bullet = structure.bullet(pos) |
| 764 | let previousBullet = previous.map { structure.bullet($0) } | |
| 765 | let alphap = previous == nil && useAlphabeticalBullets(pos, structure, prevs) | |
| 754 | 766 | let new: String |
| 755 | if let previous, let counter, counter.range(of: "[a-zA-Z]", options: .regularExpression) != nil, | |
| 756 | structure.bullet(previous).range(of: "[a-zA-Z]", options: .regularExpression) != nil { | |
| 757 | let previousBullet = structure.bullet(previous) | |
| 758 | let real = previousBullet.range(of: "[a-z]", options: .regularExpression) != nil ? counter.lowercased() : counter.uppercased() | |
| 759 | let range = previousBullet.range(of: "[a-zA-Z]", options: .regularExpression)! | |
| 760 | new = previousBullet.replacingCharacters(in: range, with: real) | |
| 761 | } else if let previous, let counter, let counterRange = counter.range(of: "[0-9]+", options: .regularExpression), | |
| 762 | let range = structure.bullet(previous).range(of: "[0-9]+", options: .regularExpression) { | |
| 763 | new = structure.bullet(previous).replacingCharacters(in: range, with: counter[counterRange]) | |
| 764 | } else if let previous { | |
| 765 | new = incrementBullet(structure.bullet(previous)) | |
| 766 | } else if let counter, let counterRange = counter.range(of: "[0-9]+", options: .regularExpression), | |
| 767 | let range = bullet.range(of: "[0-9]+", options: .regularExpression) { | |
| 767 | if previous != nil, let counter, let previousBullet, find("[a-zA-Z]", counter) != nil, find("[a-zA-Z]", previousBullet) != nil { | |
| 768 | let lower = find("[a-z]", previousBullet) | |
| 769 | let range = lower ?? find("[A-Z]", previousBullet)! | |
| 770 | new = previousBullet.replacingCharacters(in: range, with: lower != nil ? counter.lowercased() : counter.uppercased()) | |
| 771 | } else if previous != nil, let counter, let previousBullet, let counterRange = find("[0-9]+", counter), | |
| 772 | let range = find("[0-9]+", previousBullet) { | |
| 773 | new = previousBullet.replacingCharacters(in: range, with: counter[counterRange]) | |
| 774 | } else if let previousBullet { | |
| 775 | new = incrementBullet(previousBullet) | |
| 776 | } else if let counter, useAlphabeticalBullets(pos, structure, prevs), find("[A-Za-z]", counter) != nil, find("[A-Za-z]", bullet) != nil { | |
| 777 | let lower = find("[a-z]", bullet) | |
| 778 | let range = lower ?? find("[A-Z]", bullet)! | |
| 779 | new = bullet.replacingCharacters(in: range, with: lower != nil ? counter.lowercased() : counter.uppercased()) | |
| 780 | } else if let counter, let counterRange = find("[0-9]+", counter), let range = find("[0-9]+", bullet) { | |
| 768 | 781 | new = bullet.replacingCharacters(in: range, with: counter[counterRange]) |
| 769 | } else if let range = bullet.range(of: "([0-9]+|[A-Za-z])", options: .regularExpression) { | |
| 770 | stringMatched(bullet, range) | |
| 782 | } else if alphap, let range = find("[A-Z]", bullet) { | |
| 783 | new = bullet.replacingCharacters(in: range, with: "A") | |
| 784 | } else if alphap, let range = find("[a-z]", bullet) { | |
| 785 | new = bullet.replacingCharacters(in: range, with: "a") | |
| 786 | } else if let range = find("([0-9]+|[A-Za-z])", bullet) { | |
| 771 | 787 | new = bullet.replacingCharacters(in: range, with: "1") |
| 772 | 788 | } else { |
| 773 | 789 | new = bullet |
| @@ -776,6 +792,23 @@ extension EmacsBuffer { | ||
| 776 | 792 | } |
| 777 | 793 | } |
| 778 | 794 | |
| 795 | /// `org-list-use-alpha-bul-p`: the list starting at `first` fits in the alphabet. | |
| 796 | func useAlphabeticalBullets(_ first: Int, _ structure: ListStruct, _ prevs: [Int: Int]) -> Bool { | |
| 797 | guard options.listAllowAlphabetical else { return false } | |
| 798 | var item: Int? = first | |
| 799 | var ascii = 64 | |
| 800 | while let current = item { | |
| 801 | if let counter = structure.item(current)?.counter, let letter = counter.first, letter.isASCII, letter.isLetter { | |
| 802 | ascii = Int(letter.uppercased().unicodeScalars.first!.value) | |
| 803 | } else { | |
| 804 | ascii += 1 | |
| 805 | } | |
| 806 | if ascii > 90 { return false } | |
| 807 | item = structure.nextItem(current, prevs) | |
| 808 | } | |
| 809 | return true | |
| 810 | } | |
| 811 | ||
| 779 | 812 | /// `org-list-struct-fix-ind`. |
| 780 | 813 | func fixIndentation(_ structure: inout ListStruct, _ parents: [(item: Int, parent: Int?)], bulletSize: Int? = nil) { |
| 781 | 814 | let topInd = structure.ind(structure.top) |
| @@ -851,7 +884,7 @@ extension EmacsBuffer { | ||
| 851 | 884 | point = end |
| 852 | 885 | skipBackward(Self.whitespace) |
| 853 | 886 | beginningOfLine() |
| 854 | while point > begin || (point == begin && looking(Self.itemPattern) == nil) { | |
| 887 | while point > begin || (point == begin && looking(itemPattern) == nil) { | |
| 855 | 888 | if looking("^[ \\t]*\\S", setsMatch: false) != nil { |
| 856 | 889 | indentLineTo(max(currentTextIndentation + delta, ind.map { $0 + 1 } ?? -1)) |
| 857 | 890 | } |
Sources/OrgCore/Commands/Refile.swift +1 −1
| @@ -126,7 +126,7 @@ public enum Refile { | ||
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | public static func toOtherFile(source context: EditContext, target text: String, under heading: Int?) -> Result? { |
| 129 | let target = EmacsBuffer(text, point: 0) | |
| 129 | let target = EmacsBuffer(text, point: 0, settings: context.tree.settings, options: context.options) | |
| 130 | 130 | var tree = "" |
| 131 | 131 | let step = commitBuffer(context) { buffer in |
| 132 | 132 | tree = try buffer.copySubtree() |
Sources/OrgCore/Commands/StructureCommands.swift +5 −3
| @@ -74,7 +74,8 @@ extension EmacsBuffer { | ||
| 74 | 74 | saveExcursion { insert(stars + " \n") } |
| 75 | 75 | if !(blank && previousLineEmpty) { emptyLinesBeforeCurrent(blank ? 1 : 0) } |
| 76 | 76 | endOfLine() |
| 77 | } else if let match = saveExcursion({ () -> [Range<Int>?]? in beginningOfLine(); return lookingAt(complexHeadingPattern) }), | |
| 77 | } else if options.metaReturnMaySplitLine, | |
| 78 | let match = saveExcursion({ () -> [Range<Int>?]? in beginningOfLine(); return lookingAt(complexHeadingPattern) }), | |
| 78 | 79 | let title = match[4], title.lowerBound <= point, point <= title.upperBound { |
| 79 | 80 | let split = substring(point, title.upperBound) |
| 80 | 81 | deleteRegion(point, title.upperBound) |
| @@ -99,6 +100,7 @@ extension EmacsBuffer { | ||
| 99 | 100 | if !(blank && previousLineEmpty) { emptyLinesBeforeCurrent(blank ? 1 : 0) } |
| 100 | 101 | addBlankAfter(blank) |
| 101 | 102 | } else { |
| 103 | if !options.metaReturnMaySplitLine { endOfLine() } | |
| 102 | 104 | insert("\n" + stars + " ") |
| 103 | 105 | if !(blank && previousLineEmpty) { emptyLinesBeforeCurrent(blank ? 1 : 0) } |
| 104 | 106 | addBlankAfter(blank) |
| @@ -264,7 +266,7 @@ public struct InsertHeading: OrgCommand { | ||
| 264 | 266 | public var title: String { "Insert Heading" } |
| 265 | 267 | public func applies(in context: EditContext) -> Bool { true } |
| 266 | 268 | public func run(in context: EditContext) -> CommandStep { |
| 267 | headingCommand(context) { try $0.insertHeading(respectContent: false, hidden: context.hidden) } | |
| 269 | headingCommand(context) { try $0.insertHeading(respectContent: context.options.insertHeadingRespectContent, hidden: context.hidden) } | |
| 268 | 270 | } |
| 269 | 271 | } |
| 270 | 272 | |
| @@ -287,7 +289,7 @@ public struct InsertTodoHeading: OrgCommand { | ||
| 287 | 289 | public func applies(in context: EditContext) -> Bool { true } |
| 288 | 290 | public func run(in context: EditContext) -> CommandStep { |
| 289 | 291 | headingCommand(context) { buffer in |
| 290 | try buffer.insertHeading(respectContent: false, hidden: context.hidden) | |
| 292 | try buffer.insertHeading(respectContent: context.options.insertHeadingRespectContent, hidden: context.hidden) | |
| 291 | 293 | let previous = buffer.saveExcursion { () -> String? in |
| 292 | 294 | try? buffer.forwardHeadingSameLevel(-1) |
| 293 | 295 | guard let match = buffer.lookingAt(buffer.todoLinePattern), let keyword = match[2] else { return nil } |
Sources/OrgCore/Parser/Settings.swift +9
| @@ -58,6 +58,15 @@ public struct OrgSettings: Sendable, Equatable { | ||
| 58 | 58 | Set(todoSequences.flatMap { ($0.active + $0.done).map(\.name) }) |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | /// These settings with TODO keywords from `#+TODO:` syntax (`TODO NEXT | DONE`); nil when | |
| 62 | /// the line has no keywords. | |
| 63 | public func withTodoKeywords(_ line: String) -> OrgSettings? { | |
| 64 | guard let sequence = SettingsScanner.todoSequence(Substring(line), kind: .sequence) else { return nil } | |
| 65 | var copy = self | |
| 66 | copy.todoSequences = [sequence] | |
| 67 | return copy | |
| 68 | } | |
| 69 | ||
| 61 | 70 | public func isDone(_ name: String) -> Bool { |
| 62 | 71 | todoSequences.contains { $0.done.contains { $0.name == name } } |
| 63 | 72 | } |
Sources/OrgDocument/Buffer.swift +2 −1
| @@ -19,6 +19,7 @@ public protocol DocumentBuffer: AnyObject { | ||
| 19 | 19 | public final class StateBuffer: DocumentBuffer { |
| 20 | 20 | public private(set) var document: DocumentState |
| 21 | 21 | public var selection: [Range<Int>] = [0..<0] |
| 22 | public var options: EditingOptions = .org | |
| 22 | 23 | |
| 23 | 24 | public init(_ document: DocumentState) { |
| 24 | 25 | self.document = document |
| @@ -26,7 +27,7 @@ public final class StateBuffer: DocumentBuffer { | ||
| 26 | 27 | |
| 27 | 28 | public func perform(_ command: any OrgCommand, now: Date, answers: [String: String]) -> CommandStep { |
| 28 | 29 | do { |
| 29 | let step = try document.run(command, selection: selection, now: now, answers: answers) | |
| 30 | let step = try document.run(command, selection: selection, now: now, answers: answers, options: options) | |
| 30 | 31 | if case .commit(let result) = step, let new = result.selection { selection = new } |
| 31 | 32 | return step |
| 32 | 33 | } catch { |
Sources/OrgDocument/DocumentState.swift +2 −2
| @@ -101,11 +101,11 @@ public struct DocumentState: Sendable { | ||
| 101 | 101 | /// Runs `command` at `selection` and applies its edits as one undo step. |
| 102 | 102 | public mutating func run( |
| 103 | 103 | _ command: any OrgCommand, selection: [Range<Int>], now: Date = Date(), |
| 104 | calendar: Calendar = .current, answers: [String: String] = [:] | |
| 104 | calendar: Calendar = .current, answers: [String: String] = [:], options: EditingOptions = .org | |
| 105 | 105 | ) throws -> CommandStep { |
| 106 | 106 | let context = EditContext( |
| 107 | 107 | revision: revision, text: text, tree: tree, selection: selection, |
| 108 | now: now, calendar: calendar, answers: answers | |
| 108 | now: now, calendar: calendar, answers: answers, options: options | |
| 109 | 109 | ) |
| 110 | 110 | let step = command.run(in: context) |
| 111 | 111 | if case .commit(let result) = step, !result.edits.isEmpty { |
Sources/OrgEditorAppKit/OrgEditor.swift +3 −1
| @@ -187,6 +187,8 @@ public final class OrgEditor: NSObject { | ||
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | public var optionAsMeta: OptionAsMeta = [.left] |
| 190 | /// Org options commands follow. | |
| 191 | public var options: EditingOptions = .org | |
| 190 | 192 | private var dispatcher: KeyDispatcher |
| 191 | 193 | /// A region is active: movement extends the selection, as after C-SPC in Emacs. |
| 192 | 194 | private(set) var markActive = false |
| @@ -196,7 +198,7 @@ public final class OrgEditor: NSObject { | ||
| 196 | 198 | return EditContext( |
| 197 | 199 | revision: document.revision, text: document.text, tree: document.tree, |
| 198 | 200 | selection: [selected.location..<NSMaxRange(selected)], now: now, calendar: .current, answers: answers, |
| 199 | hidden: hidden.all | |
| 201 | hidden: hidden.all, options: options | |
| 200 | 202 | ) |
| 201 | 203 | } |
| 202 | 204 | |
Sources/OrgIndex/FileRecord.swift +11
| @@ -16,6 +16,17 @@ public struct IndexSettings: Sendable, Equatable { | ||
| 16 | 16 | public var org: OrgSettings |
| 17 | 17 | public var semantic: SemanticSettings |
| 18 | 18 | |
| 19 | /// Settings whose version follows the TODO keywords, so changing them reindexes. | |
| 20 | public static func with(todoKeywords org: OrgSettings) -> IndexSettings { | |
| 21 | let names = org.todoSequences.map { ($0.active.map(\.name) + ["|"] + $0.done.map(\.name)).joined(separator: " ") }.joined(separator: ";") | |
| 22 | var hash: UInt64 = 0xcbf29ce484222325 | |
| 23 | for byte in names.utf8 { | |
| 24 | hash ^= UInt64(byte) | |
| 25 | hash = hash &* 0x100000001b3 | |
| 26 | } | |
| 27 | return IndexSettings(version: 1 + Int(hash % 1_000_000), org: org) | |
| 28 | } | |
| 29 | ||
| 19 | 30 | public init(version: Int = 1, org: OrgSettings = .default, semantic: SemanticSettings = .default) { |
| 20 | 31 | self.version = version |
| 21 | 32 | self.org = org |
Tests/OrgCoreTests/EmacsOracle.swift +11 −2
| @@ -122,9 +122,18 @@ enum EmacsOracle { | ||
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | /// The options as Emacs Lisp bindings around `form`. | |
| 126 | func withOptions(_ options: EditingOptions, _ form: String) -> String { | |
| 127 | func flag(_ value: Bool) -> String { value ? "t" : "nil" } | |
| 128 | return "(let ((org-tags-column \(options.tagsColumn)) (org-insert-heading-respect-content \(flag(options.insertHeadingRespectContent))) (org-M-RET-may-split-line \(flag(options.metaReturnMaySplitLine))) (org-list-allow-alphabetical \(flag(options.listAllowAlphabetical)))) \(form))" | |
| 129 | } | |
| 130 | ||
| 131 | /// The options in the user's Doom Emacs. | |
| 132 | let doomOptions = EditingOptions(tagsColumn: 0, insertHeadingRespectContent: true, metaReturnMaySplitLine: false, listAllowAlphabetical: true) | |
| 133 | ||
| 125 | 134 | /// Runs a command the way an editor would: build a context, run, apply the edits. |
| 126 | func runCommand(_ command: any OrgCommand, _ text: String, caret: Int, answers: [String: String] = [:]) -> (text: String, caret: Int, failure: String?) { | |
| 127 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], answers: answers) | |
| 135 | func runCommand(_ command: any OrgCommand, _ text: String, caret: Int, answers: [String: String] = [:], options: EditingOptions = .org) -> (text: String, caret: Int, failure: String?) { | |
| 136 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], answers: answers, options: options) | |
| 128 | 137 | switch command.run(in: context) { |
| 129 | 138 | case .commit(let result): |
| 130 | 139 | var new = text |
Tests/OrgCoreTests/HeadingCommandTests.swift +8 −3
| @@ -66,6 +66,11 @@ struct HeadingOracleTests { | ||
| 66 | 66 | try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands) |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 70 | func headingCommandsMatchEmacsWithDoomOptions() throws { | |
| 71 | try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands, options: doomOptions) | |
| 72 | } | |
| 73 | ||
| 69 | 74 | /// Real headings: `ORGSTAR_ORACLE_CORPUS=<folder>` runs every command on up to 400 heading |
| 70 | 75 | /// lines from the folder's files, with the caret at the line start, the title start and |
| 71 | 76 | /// the line end. |
| @@ -94,7 +99,7 @@ struct HeadingOracleTests { | ||
| 94 | 99 | |
| 95 | 100 | /// Runs every command at every caret of every input here and in Emacs, and records each |
| 96 | 101 | /// difference in text, caret or failure. |
| 97 | func compareWithEmacs(_ inputs: [(text: String, carets: [Int])], _ commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)]) throws { | |
| 102 | func compareWithEmacs(_ inputs: [(text: String, carets: [Int])], _ commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)], options: EditingOptions = .org) throws { | |
| 98 | 103 | try #require(EmacsOracle.isAvailable, "Emacs is required for the oracle tests; set ORGSTAR_SKIP_ORACLE to skip") |
| 99 | 104 | var cases: [EmacsOracle.Case] = [] |
| 100 | 105 | var ours: [(label: String, text: String, caret: Int, failed: Bool)] = [] |
| @@ -102,8 +107,8 @@ func compareWithEmacs(_ inputs: [(text: String, carets: [Int])], _ commands: [(c | ||
| 102 | 107 | for offset in carets { |
| 103 | 108 | let onHeadingLine = headingOnLine(at: offset, in: OrgParser.parse(text)) != nil |
| 104 | 109 | for (command, form, headingLineOnly) in commands where onHeadingLine || !headingLineOnly { |
| 105 | cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(offset, in: text), form: form)) | |
| 106 | let result = runCommand(command, text, caret: offset) | |
| 110 | cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(offset, in: text), form: withOptions(options, form))) | |
| 111 | let result = runCommand(command, text, caret: offset, options: options) | |
| 107 | 112 | ours.append(("\(command.id) at \(offset) in \(text.debugDescription)", result.text, result.caret, result.failure != nil)) |
| 108 | 113 | } |
| 109 | 114 | } |
Tests/OrgCoreTests/ListCommandTests.swift +6
| @@ -48,6 +48,12 @@ struct ListOracleTests { | ||
| 48 | 48 | try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands) |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 52 | func listCommandsMatchEmacsWithDoomOptions() throws { | |
| 53 | let alphabetical = ["a. x\nb. y\n", "A) one\n - sub\nB) two\n", "- [@c] x\n- y\n", "1. a\n2. b\n"] | |
| 54 | try compareWithEmacs((Self.variants + alphabetical).map { ($0, EmacsOracle.positions($0)) }, Self.commands, options: doomOptions) | |
| 55 | } | |
| 56 | ||
| 51 | 57 | /// Real lists: `ORGSTAR_ORACLE_CORPUS=<folder>` runs every list command in files up to |
| 52 | 58 | /// 20 KB with the caret at the start, bullet end and end of up to six item lines per file. |
| 53 | 59 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil)) |
Tests/OrgCoreTests/StructureCommandTests.swift +5
| @@ -72,6 +72,11 @@ struct StructureOracleTests { | ||
| 72 | 72 | try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands) |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 76 | func structureCommandsMatchEmacsWithDoomOptions() throws { | |
| 77 | try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands, options: doomOptions) | |
| 78 | } | |
| 79 | ||
| 75 | 80 | /// Real files: `ORGSTAR_ORACLE_CORPUS=<folder>` runs every structure command in files up to |
| 76 | 81 | /// 20 KB, with the caret at the start and end of up to eight heading lines per file and on |
| 77 | 82 | /// a body line, for up to 60 files. |