Sources/OrgEditorAppKit/EditorCommands.swift

c2d503b97e80b560fda12d61ecc99864f2a18a25
orgstar/Sources/OrgEditorAppKit/EditorCommands.swift

282 lines · 21955 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    /// The title as the string catalog translates it.
  8    public var localizedTitle: String { CommandTitle.localized(title) }
  9
 10    public enum Action: Sendable {
 11        /// An `OrgCore` command, run through `OrgEditor.perform`.
 12        case text(any OrgCommand)
 13        /// Folding and other view changes the editor makes itself.
 14        case view
 15        /// A text-system action; `extending` is its variant that extends the selection, used
 16        /// while a mark is active.
 17        case edit(selector: String, extending: String? = nil)
 18        /// Mark handling in the editor.
 19        case mark
 20        /// The find bar.
 21        case find(NSTextFinder.Action)
 22        /// Handled by the app (save, quick open, palette).
 23        case app
 24    }
 25
 26    public let id: String
 27    public let title: String
 28    public let action: Action
 29
 30    /// Shown in the palette; text-system movement and editing are left to keys.
 31    public var inPalette: Bool {
 32        switch action {
 33        case .edit, .mark: false
 34        default: true
 35        }
 36    }
 37}
 38
 39public enum EditorCommands {
 40    /// Every command, with the named keyboard macros.
 41    public static var all: [EditorCommand] { builtIn + namedMacros(NamedMacros.shared) }
 42
 43    public static let builtIn: [EditorCommand] = Commands.everything.map { EditorCommand(id: $0.id, title: $0.title, action: .text($0)) } + [
 44        EditorCommand(id: "org.cycle", title: "Cycle Visibility", action: .view),
 45        EditorCommand(id: "org.cycle-global", title: "Cycle Global Visibility", action: .view),
 46        EditorCommand(id: "org.fold.toggle", title: "Toggle Fold", action: .view),
 47        EditorCommand(id: "org.fold.open", title: "Open Fold", action: .view),
 48        EditorCommand(id: "org.fold.close", title: "Close Fold", action: .view),
 49        EditorCommand(id: "org.fold.open-subtree", title: "Open Subtree Folds", action: .view),
 50        EditorCommand(id: "org.fold.open-all", title: "Open All Folds", action: .view),
 51        EditorCommand(id: "org.fold.close-all", title: "Close All Folds", action: .view),
 52        EditorCommand(id: "org.fold.more", title: "Show Next Fold Level", action: .view),
 53        EditorCommand(id: "org.fold.less", title: "Hide Next Fold Level", action: .view),
 54        EditorCommand(id: "org.narrow.subtree", title: "Narrow to Subtree", action: .view),
 55        EditorCommand(id: "org.narrow.block", title: "Narrow to Block", action: .view),
 56        EditorCommand(id: "org.widen", title: "Widen", action: .view),
 57        EditorCommand(id: "org.narrow.toggle", title: "Narrow to Subtree or Widen", action: .view),
 58        EditorCommand(id: "org.toggle-inline-images", title: "Show or Hide Inline Images", action: .view),
 59        EditorCommand(id: "org.toggle-inline-images-linked", title: "Show or Hide Inline Images, Including Described Links", action: .view),
 60        EditorCommand(id: "org.latex-preview", title: "Show or Hide LaTeX Previews", action: .view),
 61        EditorCommand(id: "editor.complete", title: "Complete at Point", action: .view),
 62        EditorCommand(id: "editor.dabbrev-expand", title: "Expand Word", action: .view),
 63        EditorCommand(id: "editor.dabbrev-completion", title: "Complete Word", action: .view),
 64        EditorCommand(id: "editor.dabbrev-completion-previous", title: "Complete Word, Last First", action: .view),
 65        EditorCommand(id: "org.snippet.next-field", title: "Next Snippet Field", action: .view),
 66        EditorCommand(id: "org.snippet.previous-field", title: "Previous Snippet Field", action: .view),
 67        EditorCommand(id: "app.new-file-from-template", title: "New File from Template…", action: .app),
 68        EditorCommand(id: "app.replace-in-files", title: "Replace in Files…", action: .app),
 69        EditorCommand(id: "app.rename-tag", title: "Rename Tag…", action: .app),
 70        EditorCommand(id: "app.rename-todo-keyword", title: "Rename TODO Keyword…", action: .app),
 71        EditorCommand(id: "editor.toggle-spell-check", title: "Check Spelling While Typing", action: .view),
 72        EditorCommand(id: "editor.toggle-truncate-lines", title: "Truncate or Wrap Long Lines", action: .view),
 73        EditorCommand(id: "editor.toggle-auto-fill", title: "Turn Auto Fill On or Off", action: .view),
 74        EditorCommand(id: "org.table.toggle-column-width", title: "Shrink or Expand Table Column", action: .view),
 75        EditorCommand(id: "org.table.shrink", title: "Shrink Table Columns with Widths", action: .view),
 76        EditorCommand(id: "org.table.expand", title: "Expand Table Columns", action: .view),
 77        EditorCommand(id: "org.link.insert", title: "Insert Link…", action: .app),
 78        EditorCommand(id: "org.link.store", title: "Store Link", action: .app),
 79        EditorCommand(id: "org.id.store-link", title: "Store ID Link", action: .app),
 80        EditorCommand(id: "org.subtree.paste", title: "Paste Subtree", action: .app),
 81        EditorCommand(id: "app.block.save", title: "Save Block", action: .app),
 82        EditorCommand(id: "app.block.cancel", title: "Leave Block", action: .app),
 83
 84        EditorCommand(id: "app.save", title: "Save", action: .app),
 85        EditorCommand(id: "app.quit", title: "Quit orgstar", action: .app),
 86        EditorCommand(id: "app.save-all", title: "Save All Buffers", action: .app),
 87        EditorCommand(id: "app.buffer.switch", title: "Switch to Buffer…", action: .app),
 88        EditorCommand(id: "app.buffer.kill", title: "Close Buffer", action: .app),
 89        EditorCommand(id: "app.buffer.kill-others", title: "Close Other Buffers", action: .app),
 90        EditorCommand(id: "app.buffer.kill-all", title: "Close All Buffers", action: .app),
 91        EditorCommand(id: "app.buffer.next", title: "Next Buffer", action: .app),
 92        EditorCommand(id: "app.buffer.previous", title: "Previous Buffer", action: .app),
 93        EditorCommand(id: "app.buffer.last", title: "Last Buffer", action: .app),
 94        EditorCommand(id: "app.split-below", title: "Split Pane Below", action: .app),
 95        EditorCommand(id: "app.split-right", title: "Split Pane Right", action: .app),
 96        EditorCommand(id: "app.pane-close", title: "Close Pane", action: .app),
 97        EditorCommand(id: "app.pane-only", title: "Close Other Panes", action: .app),
 98        EditorCommand(id: "app.pane-next", title: "Next Pane", action: .app),
 99        EditorCommand(id: "app.window-new", title: "New Window", action: .app),
100        EditorCommand(id: "app.babel.cancel", title: "Cancel Running Task", action: .app),
101        EditorCommand(id: "app.toggle-tab-bar", title: "Show or Hide Tab Bar", action: .app),
102        EditorCommand(id: "app.toggle-outline", title: "Show or Hide Outline", action: .app),
103        EditorCommand(id: "app.toggle-backlinks", title: "Show or Hide Backlinks", action: .app),
104        EditorCommand(id: "app.toggle-inspector", title: "Show or Hide Inspector", action: .app),
105        EditorCommand(id: "app.toggle-markup", title: "Show or Hide Markup", action: .app),
106        EditorCommand(id: "app.edit-config", title: "Edit Config File", action: .app),
107        EditorCommand(id: "app.import-emacs", title: "Import from Emacs…", action: .app),
108        EditorCommand(id: "app.toggle-line-numbers", title: "Show or Hide Line Numbers", action: .app),
109        EditorCommand(id: "app.quick-open", title: "Quick Open…", action: .app),
110        EditorCommand(id: "app.palette", title: "Command Palette…", action: .app),
111        EditorCommand(id: "app.search", title: "Search Notes", action: .app),
112        EditorCommand(id: "app.reload-keymap", title: "Reload Keymap", action: .app),
113        EditorCommand(id: "app.messages", title: "Messages", action: .app),
114        EditorCommand(id: "app.refile", title: "Refile…", action: .app),
115        EditorCommand(id: "app.refile.copy", title: "Copy to Refile Target…", action: .app),
116        EditorCommand(id: "app.refile.goto", title: "Go to Refile Target…", action: .app),
117        EditorCommand(id: "app.archive", title: "Archive Subtree", action: .app),
118        EditorCommand(id: "app.agenda", title: "Agenda", action: .app),
119        EditorCommand(id: "app.capture", title: "Capture…", action: .app),
120        EditorCommand(id: "app.board", title: "Board", action: .app),
121        EditorCommand(id: "app.clock.in", title: "Clock In", action: .app),
122        EditorCommand(id: "app.clock.in-recent", title: "Clock In to Recent Entry…", action: .app),
123        EditorCommand(id: "app.clock.in-last", title: "Clock In to Last Entry", action: .app),
124        EditorCommand(id: "app.clock.out", title: "Clock Out", action: .app),
125        EditorCommand(id: "app.clock.cancel", title: "Cancel Clock", action: .app),
126        EditorCommand(id: "app.clock.goto", title: "Go to Clocked Entry", action: .app),
127        EditorCommand(id: "app.clock.goto-recent", title: "Go to Recent Clocked Entry…", action: .app),
128        EditorCommand(id: "app.clock.default", title: "Mark as Default Clock Task", action: .app),
129        EditorCommand(id: "app.clock.resolve", title: "Resolve Open Clocks…", action: .app),
130        EditorCommand(id: "app.clock.report", title: "Clock Report", action: .app),
131        EditorCommand(id: "app.table-import", title: "Import Table from File…", action: .app),
132        EditorCommand(id: "app.table-export", title: "Export Table to File…", action: .app),
133        EditorCommand(id: "app.plot", title: "Plot Table", action: .app),
134        EditorCommand(id: "app.dailies.today", title: "Go to Today's Daily Note", action: .app),
135        EditorCommand(id: "app.dailies.yesterday", title: "Go to Yesterday's Daily Note", action: .app),
136        EditorCommand(id: "app.dailies.tomorrow", title: "Go to Tomorrow's Daily Note", action: .app),
137        EditorCommand(id: "app.dailies.date", title: "Go to Daily Note for Date…", action: .app),
138        EditorCommand(id: "app.dailies.capture-today", title: "Capture to Today's Daily Note", action: .app),
139        EditorCommand(id: "app.dailies.capture-yesterday", title: "Capture to Yesterday's Daily Note", action: .app),
140        EditorCommand(id: "app.dailies.capture-tomorrow", title: "Capture to Tomorrow's Daily Note", action: .app),
141        EditorCommand(id: "app.dailies.capture-date", title: "Capture to Daily Note for Date…", action: .app),
142        EditorCommand(id: "app.dailies.next", title: "Next Daily Note", action: .app),
143        EditorCommand(id: "app.dailies.previous", title: "Previous Daily Note", action: .app),
144        EditorCommand(id: "app.node.find", title: "Find Node…", action: .app),
145        EditorCommand(id: "app.node.insert", title: "Insert Node Link…", action: .app),
146        EditorCommand(id: "app.node.graph", title: "Show Node Graph", action: .app),
147        EditorCommand(id: "app.sidebar-agenda", title: "Show Agenda or Files in Sidebar", action: .app),
148        EditorCommand(id: "app.node.rename-from-front-matter", title: "Rename Note from Front Matter", action: .app),
149        EditorCommand(id: "app.node.rename", title: "Rename Note…", action: .app),
150        EditorCommand(id: "app.node.rename-folder-from-front-matter", title: "Rename Notes in Folder from Front Matter…", action: .app),
151        EditorCommand(id: "app.node.silo-find", title: "Find Note in Silo…", action: .app),
152        EditorCommand(id: "app.node.silo-create", title: "New Note in Silo…", action: .app),
153        EditorCommand(id: "app.node.sequence-new-parent", title: "New Sequence Note…", action: .app),
154        EditorCommand(id: "app.node.sequence-new-child", title: "New Child Sequence Note…", action: .app),
155        EditorCommand(id: "app.node.sequence-new-sibling", title: "New Sibling Sequence Note…", action: .app),
156        EditorCommand(id: "app.node.sequence-new-child-of-current", title: "New Child of This Note…", action: .app),
157        EditorCommand(id: "app.node.sequence-new-sibling-of-current", title: "New Sibling of This Note…", action: .app),
158        EditorCommand(id: "app.node.sequence-find", title: "Find Sequence Relatives…", action: .app),
159        EditorCommand(id: "app.node.sequence-next-sibling", title: "Next Sibling Note", action: .app),
160        EditorCommand(id: "app.node.sequence-previous-sibling", title: "Previous Sibling Note", action: .app),
161        EditorCommand(id: "app.node.sequence-reparent", title: "Reparent Note…", action: .app),
162        EditorCommand(id: "app.node.sequence-reparent-recursive", title: "Reparent Note with Descendants…", action: .app),
163        EditorCommand(id: "app.node.sequence-rename-as-parent", title: "Rename Note as Sequence Parent", action: .app),
164        EditorCommand(id: "app.node.sequence-convert", title: "Convert Note Sequence Scheme", action: .app),
165        EditorCommand(id: "app.node.sequence-link", title: "Insert Sequence Link…", action: .app),
166        EditorCommand(id: "app.node.sequence-hierarchy", title: "Sequence Hierarchy…", action: .app),
167        EditorCommand(id: "app.attach.attach", title: "Attach File…", action: .app),
168        EditorCommand(id: "app.attach.move", title: "Attach File by Moving…", action: .app),
169        EditorCommand(id: "app.attach.link", title: "Attach File as Hard Link…", action: .app),
170        EditorCommand(id: "app.attach.symlink", title: "Attach File as Symbolic Link…", action: .app),
171        EditorCommand(id: "app.attach.url", title: "Attach File from URL…", action: .app),
172        EditorCommand(id: "app.attach.new", title: "New Attachment…", action: .app),
173        EditorCommand(id: "app.attach.open", title: "Open Attachment…", action: .app),
174        EditorCommand(id: "app.attach.reveal", title: "Show Attachment Folder", action: .app),
175        EditorCommand(id: "app.attach.delete", title: "Delete Attachment…", action: .app),
176        EditorCommand(id: "app.attach.delete-all", title: "Delete All Attachments…", action: .app),
177        EditorCommand(id: "app.attach.sync", title: "Sync Attachments", action: .app),
178        EditorCommand(id: "app.attach.set-directory", title: "Set Attachment Folder…", action: .app),
179        EditorCommand(id: "app.attach.unset-directory", title: "Unset Attachment Folder", action: .app),
180        EditorCommand(id: "app.crypt.encrypt-entry", title: "Encrypt Entry…", action: .app),
181        EditorCommand(id: "app.crypt.open-entry", title: "Decrypt Entry…", action: .app),
182        EditorCommand(id: "app.crypt.decrypt-entry", title: "Decrypt Entry in File…", action: .app),
183        EditorCommand(id: "app.crypt.encrypt-entries", title: "Encrypt Tagged Entries…", action: .app),
184        EditorCommand(id: "app.crypt.forget-passphrase", title: "Forget Encryption Passphrase", action: .app),
185        EditorCommand(id: "app.export-dialog", title: "Export…", action: .app),
186        EditorCommand(id: "app.export-subtree", title: "Turn Subtree Export On or Off", action: .app),
187        EditorCommand(id: "app.export-body-only", title: "Turn Body-Only Export On or Off", action: .app),
188        EditorCommand(id: "app.share-subtree", title: "Share Subtree…", action: .app),
189        EditorCommand(id: "app.sync-conflicts", title: "Resolve Sync Conflicts…", action: .app),
190        EditorCommand(id: "app.revert", title: "Revert to File on Disk", action: .app),
191        EditorCommand(id: "app.recovery", title: "History…", action: .app),
192        EditorCommand(id: "app.lint", title: "Lint File", action: .app),
193        EditorCommand(id: "app.lint-folders", title: "Lint All Folders", action: .app),
194        EditorCommand(id: "app.export.html", title: "Export to HTML", action: .app),
195        EditorCommand(id: "app.export.html-open", title: "Export to HTML and Open", action: .app),
196        EditorCommand(id: "app.export.markdown", title: "Export to Markdown", action: .app),
197        EditorCommand(id: "app.export.print-pdf", title: "Export to PDF", action: .app),
198        EditorCommand(id: "app.export.docx", title: "Export to Word", action: .app),
199        EditorCommand(id: "app.export.rtf", title: "Export to RTF", action: .app),
200        EditorCommand(id: "app.export.plain-text", title: "Export to Plain Text", action: .app),
201        EditorCommand(id: "app.copy-rich-text", title: "Copy as Rich Text", action: .app),
202        EditorCommand(id: "app.export.pdf", title: "Export to PDF with Emacs", action: .app),
203        EditorCommand(id: "app.export.odt", title: "Export to ODT", action: .app),
204        EditorCommand(id: "app.export.latex", title: "Export to LaTeX", action: .app),
205        EditorCommand(id: "app.export.text", title: "Export to Plain Text (ASCII)", action: .app),
206        EditorCommand(id: "app.export.icalendar", title: "Export to iCalendar", action: .app),
207        EditorCommand(id: "app.export.icalendar-agenda", title: "Export Agenda Files to iCalendar", action: .app),
208        EditorCommand(id: "app.export.icalendar-combined", title: "Combine Agenda Files into iCalendar", action: .app),
209        EditorCommand(id: "app.publish", title: "Publish Project…", action: .app),
210        EditorCommand(id: "app.publish.all", title: "Publish All Projects", action: .app),
211        EditorCommand(id: "app.publish.current-file", title: "Publish Current File", action: .app),
212        EditorCommand(id: "app.publish.current-project", title: "Publish Current Project", action: .app),
213
214        movement("forward-char", "Forward Character", "moveForward:"),
215        movement("backward-char", "Backward Character", "moveBackward:"),
216        movement("next-line", "Next Line", "moveDown:"),
217        movement("previous-line", "Previous Line", "moveUp:"),
218        movement("beginning-of-line", "Beginning of Line", "moveToBeginningOfParagraph:"),
219        movement("end-of-line", "End of Line", "moveToEndOfParagraph:"),
220        movement("forward-word", "Forward Word", "moveWordForward:"),
221        movement("backward-word", "Backward Word", "moveWordBackward:"),
222        movement("beginning-of-buffer", "Beginning of Buffer", "moveToBeginningOfDocument:"),
223        movement("end-of-buffer", "End of Buffer", "moveToEndOfDocument:"),
224        movement("scroll-up", "Scroll Up", "pageDown:"),
225        movement("scroll-down", "Scroll Down", "pageUp:"),
226        edit("recenter", "Recenter", "centerSelectionInVisibleArea:"),
227        edit("delete-char", "Delete Character", "deleteForward:"),
228        edit("kill-word", "Kill Word", "deleteWordForward:"),
229        edit("backward-kill-word", "Kill Word Backward", "deleteWordBackward:"),
230        edit("kill-line", "Kill Line", "deleteToEndOfParagraph:"),
231        edit("yank", "Yank", "yank:"),
232        edit("yank-media", "Yank Media", "paste:"),
233        edit("select-all", "Select All", "selectAll:"),
234        edit("kill-region", "Kill Region", "cut:"),
235        edit("copy-region", "Copy Region", "copy:"),
236        edit("undo", "Undo", "undo:"),
237        edit("transpose-chars", "Transpose Characters", "transpose:"),
238        EditorCommand(id: "edit.find", title: "Find", action: .find(.showFindInterface)),
239        EditorCommand(id: "edit.isearch-forward", title: "Incremental Search", action: .view),
240        EditorCommand(id: "edit.isearch-backward", title: "Incremental Search Backward", action: .view),
241        EditorCommand(id: "edit.isearch-forward-regexp", title: "Incremental Regexp Search", action: .view),
242        EditorCommand(id: "edit.isearch-backward-regexp", title: "Incremental Regexp Search Backward", action: .view),
243        EditorCommand(id: "edit.replace", title: "Find and Replace", action: .find(.showReplaceInterface)),
244        EditorCommand(id: "edit.find-next", title: "Find Next", action: .find(.nextMatch)),
245        EditorCommand(id: "edit.find-previous", title: "Find Previous", action: .find(.previousMatch)),
246        EditorCommand(id: "edit.use-selection-for-find", title: "Use Selection for Find", action: .find(.setSearchString)),
247        EditorCommand(id: "edit.set-mark", title: "Set Mark", action: .mark),
248        EditorCommand(id: "edit.keyboard-quit", title: "Quit", action: .mark),
249
250        EditorCommand(id: "macro.start", title: "Start Recording Macro", action: .view),
251        EditorCommand(id: "macro.end", title: "Stop Recording Macro", action: .view),
252        EditorCommand(id: "macro.call", title: "Run Last Macro", action: .view),
253        EditorCommand(id: "macro.start-or-insert-counter", title: "Start Macro or Insert Counter", action: .view),
254        EditorCommand(id: "macro.end-or-call", title: "Stop Macro or Run Last Macro", action: .view),
255        EditorCommand(id: "macro.name", title: "Name Last Macro…", action: .view),
256        EditorCommand(id: "macro.run", title: "Run Macro…", action: .view),
257    ]
258
259    /// A command for each named macro, titled with its name.
260    public static func namedMacros(_ store: NamedMacros) -> [EditorCommand] {
261        store.names.map { EditorCommand(id: NamedMacros.commandPrefix + $0, title: $0, action: .view) }
262    }
263
264    private static func movement(_ name: String, _ title: String, _ selector: String) -> EditorCommand {
265        let extending = String(selector.dropLast()) + "AndModifySelection:"
266        return EditorCommand(id: "edit.\(name)", title: title, action: .edit(selector: selector, extending: extending))
267    }
268
269    private static func edit(_ name: String, _ title: String, _ selector: String) -> EditorCommand {
270        EditorCommand(id: "edit.\(name)", title: title, action: .edit(selector: selector))
271    }
272
273    private static let byID = Dictionary(uniqueKeysWithValues: builtIn.map { ($0.id, $0) })
274
275    /// A command by id; any `macro.named.` id is one, which says so when no macro has its name.
276    public static func command(_ id: String) -> EditorCommand? {
277        if let command = byID[id] { return command }
278        guard id.hasPrefix(NamedMacros.commandPrefix) else { return nil }
279        return EditorCommand(id: id, title: String(id.dropFirst(NamedMacros.commandPrefix.count)), action: .view)
280    }
281}
282#endif
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