Sources/OrgEditorAppKit/EditorCommands.swift

71333253c9544b857c22f69f56d3af166b7ef573
orgstar/Sources/OrgEditorAppKit/EditorCommands.swift history · blame · raw

144 lines · 9308 bytes

  1#if os(macOS)
  2import AppKit
  3import OrgCore
  4
  5/// Every command a key, menu or the palette can run, by id.
  6public struct EditorCommand: Sendable {
  7    public enum Action: Sendable {
  8        /// An `OrgCore` command, run through `OrgEditor.perform`.
  9        case text(any OrgCommand)
 10        /// Folding and other view changes the editor makes itself.
 11        case view
 12        /// A text-system action; `extending` is its variant that extends the selection, used
 13        /// while a mark is active.
 14        case edit(selector: String, extending: String? = nil)
 15        /// Mark handling in the editor.
 16        case mark
 17        /// The find bar.
 18        case find(NSTextFinder.Action)
 19        /// Handled by the app (save, quick open, palette).
 20        case app
 21    }
 22
 23    public let id: String
 24    public let title: String
 25    public let action: Action
 26
 27    /// Shown in the palette; text-system movement and editing are left to keys.
 28    public var inPalette: Bool {
 29        switch action {
 30        case .edit, .mark: false
 31        default: true
 32        }
 33    }
 34}
 35
 36public enum EditorCommands {
 37    public static let all: [EditorCommand] = Commands.everything.map { EditorCommand(id: $0.id, title: $0.title, action: .text($0)) } + [
 38        EditorCommand(id: "org.cycle", title: "Cycle Visibility", action: .view),
 39        EditorCommand(id: "org.cycle-global", title: "Cycle Global Visibility", action: .view),
 40        EditorCommand(id: "org.narrow.subtree", title: "Narrow to Subtree", action: .view),
 41        EditorCommand(id: "org.narrow.block", title: "Narrow to Block", action: .view),
 42        EditorCommand(id: "org.widen", title: "Widen", action: .view),
 43        EditorCommand(id: "org.narrow.toggle", title: "Narrow to Subtree or Widen", action: .view),
 44        EditorCommand(id: "org.toggle-inline-images", title: "Show or Hide Inline Images", action: .view),
 45        EditorCommand(id: "org.table.toggle-column-width", title: "Shrink or Expand Table Column", action: .view),
 46        EditorCommand(id: "org.table.shrink", title: "Shrink Table Columns with Widths", action: .view),
 47        EditorCommand(id: "org.table.expand", title: "Expand Table Columns", action: .view),
 48        EditorCommand(id: "org.link.insert", title: "Insert Link…", action: .app),
 49        EditorCommand(id: "org.link.store", title: "Store Link", action: .app),
 50        EditorCommand(id: "org.id.store-link", title: "Store ID Link", action: .app),
 51        EditorCommand(id: "org.subtree.paste", title: "Paste Subtree", action: .app),
 52        EditorCommand(id: "app.block.save", title: "Save Block", action: .app),
 53        EditorCommand(id: "app.block.cancel", title: "Leave Block", action: .app),
 54
 55        EditorCommand(id: "app.save", title: "Save", action: .app),
 56        EditorCommand(id: "app.quit", title: "Quit Orgstar", action: .app),
 57        EditorCommand(id: "app.save-all", title: "Save All Buffers", action: .app),
 58        EditorCommand(id: "app.buffer.switch", title: "Switch to Buffer…", action: .app),
 59        EditorCommand(id: "app.buffer.kill", title: "Close Buffer", action: .app),
 60        EditorCommand(id: "app.buffer.kill-others", title: "Close Other Buffers", action: .app),
 61        EditorCommand(id: "app.buffer.kill-all", title: "Close All Buffers", action: .app),
 62        EditorCommand(id: "app.buffer.next", title: "Next Buffer", action: .app),
 63        EditorCommand(id: "app.buffer.previous", title: "Previous Buffer", action: .app),
 64        EditorCommand(id: "app.buffer.last", title: "Last Buffer", action: .app),
 65        EditorCommand(id: "app.babel.cancel", title: "Cancel Running Block", action: .app),
 66        EditorCommand(id: "app.toggle-tab-bar", title: "Show or Hide Tab Bar", action: .app),
 67        EditorCommand(id: "app.toggle-outline", title: "Show or Hide Outline", action: .app),
 68        EditorCommand(id: "app.toggle-markup", title: "Show or Hide Markup", action: .app),
 69        EditorCommand(id: "app.edit-config", title: "Edit Config File", action: .app),
 70        EditorCommand(id: "app.import-emacs", title: "Import from Emacs…", action: .app),
 71        EditorCommand(id: "app.toggle-line-numbers", title: "Show or Hide Line Numbers", action: .app),
 72        EditorCommand(id: "app.quick-open", title: "Quick Open…", action: .app),
 73        EditorCommand(id: "app.palette", title: "Command Palette…", action: .app),
 74        EditorCommand(id: "app.search", title: "Search Notes", action: .app),
 75        EditorCommand(id: "app.reload-keymap", title: "Reload Keymap", action: .app),
 76        EditorCommand(id: "app.refile", title: "Refile…", action: .app),
 77        EditorCommand(id: "app.archive", title: "Archive Subtree", action: .app),
 78        EditorCommand(id: "app.agenda", title: "Agenda", action: .app),
 79        EditorCommand(id: "app.capture", title: "Capture…", action: .app),
 80        EditorCommand(id: "app.board", title: "Board", action: .app),
 81        EditorCommand(id: "app.clock.in", title: "Clock In", action: .app),
 82        EditorCommand(id: "app.clock.out", title: "Clock Out", action: .app),
 83        EditorCommand(id: "app.clock.cancel", title: "Cancel Clock", action: .app),
 84        EditorCommand(id: "app.clock.goto", title: "Go to Clocked Entry", action: .app),
 85        EditorCommand(id: "app.clock.report", title: "Clock Report", action: .app),
 86        EditorCommand(id: "app.table-import", title: "Import Table from File…", action: .app),
 87        EditorCommand(id: "app.table-export", title: "Export Table to File…", action: .app),
 88        EditorCommand(id: "app.export-dialog", title: "Export…", action: .app),
 89        EditorCommand(id: "app.sync-conflicts", title: "Resolve Sync Conflicts…", action: .app),
 90        EditorCommand(id: "app.recovery", title: "Recovery Versions…", action: .app),
 91        EditorCommand(id: "app.export.html", title: "Export to HTML", action: .app),
 92        EditorCommand(id: "app.export.html-open", title: "Export to HTML and Open", action: .app),
 93        EditorCommand(id: "app.export.markdown", title: "Export to Markdown", action: .app),
 94        EditorCommand(id: "app.export.pdf", title: "Export to PDF with Emacs", action: .app),
 95        EditorCommand(id: "app.export.odt", title: "Export to ODT with Emacs", action: .app),
 96        EditorCommand(id: "app.export.latex", title: "Export to LaTeX with Emacs", action: .app),
 97        EditorCommand(id: "app.export.text", title: "Export to Plain Text with Emacs", action: .app),
 98
 99        movement("forward-char", "Forward Character", "moveForward:"),
100        movement("backward-char", "Backward Character", "moveBackward:"),
101        movement("next-line", "Next Line", "moveDown:"),
102        movement("previous-line", "Previous Line", "moveUp:"),
103        movement("beginning-of-line", "Beginning of Line", "moveToBeginningOfParagraph:"),
104        movement("end-of-line", "End of Line", "moveToEndOfParagraph:"),
105        movement("forward-word", "Forward Word", "moveWordForward:"),
106        movement("backward-word", "Backward Word", "moveWordBackward:"),
107        movement("beginning-of-buffer", "Beginning of Buffer", "moveToBeginningOfDocument:"),
108        movement("end-of-buffer", "End of Buffer", "moveToEndOfDocument:"),
109        movement("scroll-up", "Scroll Up", "pageDown:"),
110        movement("scroll-down", "Scroll Down", "pageUp:"),
111        edit("recenter", "Recenter", "centerSelectionInVisibleArea:"),
112        edit("delete-char", "Delete Character", "deleteForward:"),
113        edit("kill-word", "Kill Word", "deleteWordForward:"),
114        edit("backward-kill-word", "Kill Word Backward", "deleteWordBackward:"),
115        edit("kill-line", "Kill Line", "deleteToEndOfParagraph:"),
116        edit("yank", "Yank", "yank:"),
117        edit("select-all", "Select All", "selectAll:"),
118        edit("kill-region", "Kill Region", "cut:"),
119        edit("copy-region", "Copy Region", "copy:"),
120        edit("undo", "Undo", "undo:"),
121        edit("transpose-chars", "Transpose Characters", "transpose:"),
122        EditorCommand(id: "edit.find", title: "Find", action: .find(.showFindInterface)),
123        EditorCommand(id: "edit.replace", title: "Find and Replace", action: .find(.showReplaceInterface)),
124        EditorCommand(id: "edit.find-next", title: "Find Next", action: .find(.nextMatch)),
125        EditorCommand(id: "edit.find-previous", title: "Find Previous", action: .find(.previousMatch)),
126        EditorCommand(id: "edit.use-selection-for-find", title: "Use Selection for Find", action: .find(.setSearchString)),
127        EditorCommand(id: "edit.set-mark", title: "Set Mark", action: .mark),
128        EditorCommand(id: "edit.keyboard-quit", title: "Quit", action: .mark),
129    ]
130
131    private static func movement(_ name: String, _ title: String, _ selector: String) -> EditorCommand {
132        let extending = String(selector.dropLast()) + "AndModifySelection:"
133        return EditorCommand(id: "edit.\(name)", title: title, action: .edit(selector: selector, extending: extending))
134    }
135
136    private static func edit(_ name: String, _ title: String, _ selector: String) -> EditorCommand {
137        EditorCommand(id: "edit.\(name)", title: title, action: .edit(selector: selector))
138    }
139
140    private static let byID = Dictionary(uniqueKeysWithValues: all.map { ($0.id, $0) })
141
142    public static func command(_ id: String) -> EditorCommand? { byID[id] }
143}
144#endif