Sources/OrgCore/Commands/Dwim.swift

49c5da97098ed000be935c19462d4102812b0471
orgstar/Sources/OrgCore/Commands/Dwim.swift

44 lines · 2472 bytes

3 symbols in this file
 1import Foundation
 2
 3/// Doom's `+org/dwim-at-point` (RET in normal state): on a heading, TODO becomes the first
 4/// done keyword of its sequence and a done keyword the first TODO one; on an item, its
 5/// checkbox toggles; in a src block, the block runs; on a `#+TBLFM` line or in a table with
 6/// formulas, the table recalculates, otherwise aligns.
 7public struct OrgDwim: OrgCommand {
 8    public init() {}
 9    public var id: String { "org.dwim" }
10    public var title: String { "Act at Point" }
11
12    public func applies(in context: EditContext) -> Bool { true }
13
14    public func run(in context: EditContext) -> CommandStep {
15        // `+org/dwim-at-point` follows a link or citation first.
16        if Links.link(at: context.caret, text: context.text, tree: context.tree) != nil || Links.citation(at: context.caret, text: context.text, tree: context.tree) != nil {
17            return OpenLinkAtPoint().run(in: context)
18        }
19        if TableRecalculate.tblfmLine(in: context) != nil { return TableRecalculate(all: true).run(in: context) }
20        if KeyContext.holds("src", in: context) { return ExecuteSrcBlock().run(in: context) }
21        if KeyContext.holds("table", in: context) {
22            let buffer = EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings)
23            if buffer.firstTblfm(after: buffer.tableEnd()) != nil { return TableRecalculate(all: true).run(in: context) }
24            return TableCommand(.align).run(in: context)
25        }
26        if KeyContext.holds("item", in: context) {
27            let toggle = ToggleCheckbox()
28            if toggle.applies(in: context) { return toggle.run(in: context) }
29        }
30        guard headingOnLine(at: context.caret, in: context.tree) != nil else { return .failed("Nothing to do here") }
31        let settings = context.tree.settings
32        return commitBuffer(context) { buffer in
33            guard let current = buffer.todoKeyword,
34                  let sequence = settings.todoSequences.first(where: { ($0.active + $0.done).contains { $0.name == current } }) else { return }
35            if settings.isDone(current) {
36                buffer.setTodoKeyword(sequence.active.first?.name)
37            } else if let done = sequence.done.first?.name {
38                try buffer.checkTodoBlocking(from: current, to: done)
39                buffer.setTodoKeyword(done)
40            }
41            try buffer.afterTodoChange(previous: current, answers: context.answers)
42        }
43    }
44}
Keyboard shortcuts

Global

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

Repository & editor

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