Sources/OrgCore/Commands/Command.swift

d32d29c0620e94a09a204185d1a2004a8d58c518
orgstar/Sources/OrgCore/Commands/Command.swift history · blame · raw

279 lines · 14113 bytes

  1import Foundation
  2
  3/// Org options that change what commands write, as the user's Emacs has them.
  4public 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    /// `org-log-repeat` `time`: completing a repeating entry records LAST_REPEAT and a state note.
 15    public var logRepeat: Bool
 16    /// `org-log-done`: CLOSED when an entry becomes done, and a note with `.note`.
 17    public var logDone: LogHow?
 18    /// `org-log-into-drawer`: the drawer state notes go in, or nil for none.
 19    public var logIntoDrawer: String?
 20    /// `org-log-reschedule` and `org-log-redeadline`: a note when a date changes or goes.
 21    public var logReschedule: LogHow?
 22    public var logRedeadline: LogHow?
 23    /// `org-use-fast-todo-selection` `auto`: with keys in the keywords, C-c C-t asks for one.
 24    public var fastTodoSelection: Bool
 25    /// `org-use-speed-commands`: single keys at the start of a heading line run commands.
 26    public var useSpeedCommands: Bool
 27    /// `org-startup-with-inline-images`.
 28    public var startupWithInlineImages: Bool
 29    /// `org-startup-indented`: org-indent's virtual indentation.
 30    public var startupIndented = true
 31    /// `org-hide-leading-stars`; org-indent hides them too.
 32    public var hideLeadingStars = false
 33    /// `org-startup-align-all-tables`.
 34    public var startupAlignAllTables = false
 35    /// `org-startup-truncated`: long lines run off the right edge instead of wrapping.
 36    public var startupTruncated = false
 37    /// Spell checking while typing, outside code and markup.
 38    public var spellCheck = false
 39    /// `electric-pair-mode`: brackets and quotes typed in pairs.
 40    public var electricPair = false
 41    /// `org-cycle-hide-drawer-startup`: drawers are folded when a file opens.
 42    public var hideDrawerStartup: Bool
 43    /// `org-cycle-hide-block-startup`: blocks are folded when a file opens.
 44    public var hideBlockStartup: Bool
 45    /// `fill-column` for M-q.
 46    public var fillColumn: Int
 47    /// `org-hide-emphasis-markers`: table alignment measures emphasis without its markers.
 48    public var hideEmphasisMarkers: Bool
 49    /// `org-pretty-entities`: table alignment measures `\alpha` as `α`.
 50    public var prettyEntities: Bool
 51    /// `org-use-sub-superscripts` `{}`: only `_{…}` and `^{…}` are sub- and superscripts.
 52    public var subSuperscriptsNeedBraces: Bool
 53
 54    public init(
 55        tagsColumn: Int = -77, insertHeadingRespectContent: Bool = false, metaReturnMaySplitLine: Bool = true,
 56        listAllowAlphabetical: Bool = false, logRepeat: Bool = true, fillColumn: Int = 70,
 57        hideEmphasisMarkers: Bool = false, prettyEntities: Bool = false, subSuperscriptsNeedBraces: Bool = false,
 58        logDone: LogHow? = nil, logIntoDrawer: String? = nil, fastTodoSelection: Bool = true,
 59        hideDrawerStartup: Bool = true, hideBlockStartup: Bool = false, logReschedule: LogHow? = nil, logRedeadline: LogHow? = nil,
 60        useSpeedCommands: Bool = false, startupWithInlineImages: Bool = false,
 61        startupIndented: Bool = true, hideLeadingStars: Bool = false, startupAlignAllTables: Bool = false,
 62        startupTruncated: Bool = false, spellCheck: Bool = false
 63    ) {
 64        self.startupTruncated = startupTruncated
 65        self.spellCheck = spellCheck
 66        self.startupIndented = startupIndented
 67        self.hideLeadingStars = hideLeadingStars
 68        self.startupAlignAllTables = startupAlignAllTables
 69        self.useSpeedCommands = useSpeedCommands
 70        self.startupWithInlineImages = startupWithInlineImages
 71        self.logReschedule = logReschedule
 72        self.logRedeadline = logRedeadline
 73        self.hideDrawerStartup = hideDrawerStartup
 74        self.hideBlockStartup = hideBlockStartup
 75        self.fastTodoSelection = fastTodoSelection
 76        self.logDone = logDone
 77        self.logIntoDrawer = logIntoDrawer
 78        self.tagsColumn = tagsColumn
 79        self.insertHeadingRespectContent = insertHeadingRespectContent
 80        self.metaReturnMaySplitLine = metaReturnMaySplitLine
 81        self.listAllowAlphabetical = listAllowAlphabetical
 82        self.logRepeat = logRepeat
 83        self.fillColumn = fillColumn
 84        self.hideEmphasisMarkers = hideEmphasisMarkers
 85        self.prettyEntities = prettyEntities
 86        self.subSuperscriptsNeedBraces = subSuperscriptsNeedBraces
 87    }
 88
 89    /// Org's own defaults.
 90    public static let org = EditingOptions()
 91}
 92
 93/// Everything a command may read. Commands never read the clock or the file system, so the
 94/// same context always gives the same result.
 95public struct EditContext: Sendable {
 96    /// The revision `text` and `tree` belong to; results are applied only at this revision.
 97    public let revision: Int
 98    public let text: String
 99    public let tree: OrgTree
100    /// Selections in UTF-16 offsets. The first is the main caret.
101    public let selection: [Range<Int>]
102    public let now: Date
103    /// Includes the time zone.
104    public let calendar: Calendar
105    /// Replies to earlier prompts, by prompt key.
106    public let answers: [String: String]
107    /// Ranges the view hides (folded text); motion commands skip headings inside them.
108    public let hidden: [Range<Int>]
109    public let options: EditingOptions
110    /// Tags used across the workspace, for completion; read when a command needs them.
111    public let knownTags: @Sendable () -> [String]
112    /// The command run just before, with nothing typed since (`last-command`).
113    public let lastCommand: String?
114
115    public init(
116        revision: Int, text: String, tree: OrgTree, selection: [Range<Int>],
117        now: Date = Date(), calendar: Calendar = .current, answers: [String: String] = [:], hidden: [Range<Int>] = [],
118        options: EditingOptions = .org, knownTags: @escaping @Sendable () -> [String] = { [] }, lastCommand: String? = nil
119    ) {
120        self.lastCommand = lastCommand
121        self.knownTags = knownTags
122        self.revision = revision
123        self.text = text
124        self.tree = tree
125        self.selection = selection
126        self.now = now
127        self.calendar = calendar
128        self.answers = answers
129        self.hidden = hidden
130        self.options = options
131    }
132
133    public var caret: Int { selection.first?.lowerBound ?? 0 }
134}
135
136public struct Prompt: Sendable, Equatable {
137    /// The key the answer comes back under in `EditContext.answers`.
138    public let key: String
139    public let message: String
140    /// Text the answer field starts with.
141    public let initial: String
142    /// When set, the answer is one of these, or with `requireMatch` false, these are offered.
143    public let choices: [String]
144    public let requireMatch: Bool
145    /// Choices complete the text after the last of these characters (`:` between tags).
146    public let completionSeparator: Character?
147    /// Single keys answer instead of text.
148    public let fast: FastSelection?
149    /// A date prompt, read by `org-read-date` against this default (nil: today).
150    public let date: DateRead?
151
152    public struct DateRead: Sendable, Equatable {
153        public let defaultDate: Date?
154        public init(defaultDate: Date?) { self.defaultDate = defaultDate }
155    }
156
157    public init(
158        key: String, message: String, initial: String = "", choices: [String] = [], requireMatch: Bool = true,
159        completionSeparator: Character? = nil, fast: FastSelection? = nil, date: DateRead? = nil
160    ) {
161        self.key = key
162        self.message = message
163        self.initial = initial
164        self.choices = choices
165        self.requireMatch = requireMatch
166        self.completionSeparator = completionSeparator
167        self.fast = fast
168        self.date = date
169    }
170}
171
172/// Edits computed elsewhere (refiling to another file, archiving), applied like any command's
173/// result. Fails if the document changed since they were computed.
174public struct PrecomputedEdit: OrgCommand {
175    public let result: EditResult
176    public init(_ result: EditResult) { self.result = result }
177    public var id: String { "internal.precomputed" }
178    public var title: String { "Apply Edit" }
179    public func applies(in context: EditContext) -> Bool { context.revision == result.baseRevision }
180    public func run(in context: EditContext) -> CommandStep {
181        context.revision == result.baseRevision ? .commit(result) : .failed("The file changed; try again.")
182    }
183}
184
185/// Things a command asks for besides text changes; the platform layer carries them out.
186public enum Effect: Sendable, Equatable {
187    case message(String)
188    /// An inserted link was a stored one; it leaves `org-stored-links`.
189    case storedLinkUsed(String)
190    /// Text for the clipboard (the kill ring).
191    case copy(String)
192    /// Show a sparse tree: the context of each point, the rest folded, and highlights until
193    /// the next edit or C-c C-c.
194    case sparseTree(show: [Int], highlights: [Range<Int>])
195    /// Fold the subtree of the heading starting here (in the new text).
196    case fold(Int)
197}
198
199public struct EditResult: Sendable, Equatable {
200    public let baseRevision: Int
201    /// Non-overlapping, in `baseRevision` coordinates.
202    public let edits: [TextEdit]
203    /// Selection after the edits, in new coordinates; nil maps the old selection through.
204    public let selection: [Range<Int>]?
205    public let effects: [Effect]
206
207    public init(baseRevision: Int, edits: [TextEdit], selection: [Range<Int>]? = nil, effects: [Effect] = []) {
208        self.baseRevision = baseRevision
209        self.edits = edits
210        self.selection = selection
211        self.effects = effects
212    }
213}
214
215public enum CommandStep: Sendable, Equatable {
216    case commit(EditResult)
217    /// Ask, then run again with the answer in `EditContext.answers`.
218    case prompt(Prompt)
219    /// The command can't run here; nothing changes. The message is for the user, as org's
220    /// `user-error`.
221    case failed(String)
222    /// Work the platform does outside the text (Emacs, an interpreter); its result comes back
223    /// as an edit.
224    case external(ExternalRequest)
225}
226
227public enum ExternalRequest: Sendable, Equatable {
228    /// Recalculate the table whose text is at `table` in Emacs, running `form` with point at
229    /// `point`, and put Emacs's table text in its place. `lisp` when its formulas run Lisp.
230    case emacsTable(table: Range<Int>, point: Int, form: String, reason: String, lisp: Bool)
231    /// Run a src block; `Babel.finish` turns its output into the result edit.
232    case babel(BabelJob)
233    /// Follow a link: the app opens URLs and files and searches headings.
234    case openLink(OrgLink)
235    /// Edit a block's text apart (`org-edit-special`); `ReplaceBlockLines` puts it back.
236    case editBlock(BlockEdit)
237    /// Show a column view.
238    case columnView(ColumnView.View)
239    /// Show the agenda for `days` days from `start` (a `Days` number), as
240    /// `org-follow-timestamp-link` does.
241    case agenda(start: Int, days: Int)
242}
243
244/// A named operation. Keys, menus, the palette and touch controls all run commands.
245public protocol OrgCommand: Sendable {
246    /// Stable identifier, used by keymaps: `org.todo.cycle`.
247    var id: String { get }
248    /// Shown in the command palette.
249    var title: String { get }
250    /// Whether the command means something at the caret; context dispatch (one key, several
251    /// commands) runs the first that applies.
252    func applies(in context: EditContext) -> Bool
253    func run(in context: EditContext) -> CommandStep
254}
255
256public enum Commands {
257    public static let all: [any OrgCommand] = [
258        TodoCycle(), TodoShift(right: true), TodoShift(right: false), PriorityUp(), PriorityDown(), PromoteHeading(), DemoteHeading(),
259        InsertHeading(), InsertHeadingAfterSubtree(), InsertTodoHeading(),
260        MoveSubtreeUp(), MoveSubtreeDown(), PromoteSubtree(), DemoteSubtree(),
261        NextHeading(), PreviousHeading(), ForwardSameLevel(), BackwardSameLevel(), UpHeading(),
262        SetTags(),
263        InsertItem(), InsertCheckboxItem(), IndentItem(), OutdentItem(), IndentItemTree(), OutdentItemTree(),
264        MoveItemUp(), MoveItemDown(), ToggleCheckbox(),
265        InsertTimestamp(), InsertTimestamp(inactive: true), SetPlanning(deadline: false), SetPlanning(deadline: true),
266        RemovePlanning(deadline: false), RemovePlanning(deadline: true), FillParagraph(), CreateTable(),
267        TableRecalculate(all: false), TableRecalculate(all: true), ExecuteSrcBlock(), OrgDwim(),
268        ShiftTimestamp(up: true), ShiftTimestamp(up: false), ShiftTimestampDay(later: true), ShiftTimestampDay(later: false),
269        OpenLinkAtPoint(), CreateID(), ToggleComment(), ToggleHeading(), ToggleItem(), MarkSubtree(),
270        CopySubtree(cut: false), CopySubtree(cut: true), CloneSubtree(), SortEntries(), InsertStructureTemplate(), EditSpecial(), TableEditField(),
271        UpdateDynamicBlock(), ClockReportTable(), SetEffort(), CtrlCCtrlC(), EvaluateTimeRange(), FootnoteAction(), FootnoteAction(special: true), TableEvalFormula(named: false), TableEvalFormula(named: true), SortTableLines(), TransposeTable(), GotoHeading(), SparseTreeCommand(), SetPriority(), SetPriority(value: "A"), SetPriority(value: "B"), SetPriority(value: "C"), SetPriority(value: " "), ReadAndSetProperty(), DeleteProperty(), DeleteProperty(globally: true), PropertyAction(), NextAllowedValue(previous: false), NextAllowedValue(previous: true), InsertColumnView(), ShowColumnView(), ShowColumnView(global: true), ToggleArchiveTag(), ArchiveToSibling(),
272    ]
273
274    public static var everything: [any OrgCommand] { all + TableCommand.Action.allCases.map(TableCommand.init) }
275
276    public static func command(_ id: String) -> (any OrgCommand)? {
277        everything.first { $0.id == id }
278    }
279}