Sources/OrgCore/Keymap/Presets.swift

5eef7a168632da902fdd0979d57258320e4fdfc2
orgstar/Sources/OrgCore/Keymap/Presets.swift history · blame · raw

81 lines · 3247 bytes

 1/// The keymaps the app ships. The user's `keymap.toml` is layered on top of the chosen one.
 2public enum KeymapPreset: String, Sendable, CaseIterable {
 3    case emacs, mac, doom
 4
 5    public var title: String {
 6        switch self {
 7        case .emacs: "Emacs"
 8        case .mac: "Mac"
 9        case .doom: "Doom (Vim keys)"
10        }
11    }
12
13    /// Whether the preset edits modally.
14    public var isModal: Bool { self == .doom }
15
16    public var keymap: Keymap {
17        var problems: [String] = []
18        let map = try! Keymap(toml: toml, problems: &problems)
19        precondition(problems.isEmpty, "preset \(rawValue): \(problems)")
20        return map
21    }
22
23    public var toml: String {
24        switch self {
25        case .emacs: Self.common + Self.speedBindings(mode: nil) + Self.emacsBindings
26        case .mac: Self.common + Self.speedBindings(mode: nil) + Self.macBindings
27        case .doom: Self.doomToml + Self.speedBindings(mode: "insert")
28        }
29    }
30
31    /// `org-speed-commands`, for the start of a heading line when `org-use-speed-commands` is on.
32    static let speedCommands: [(key: String, command: String)] = [
33        ("n", "org.heading.next"), ("p", "org.heading.previous"), ("f", "org.heading.forward-same-level"),
34        ("b", "org.heading.backward-same-level"), ("u", "org.heading.up"), ("j", "org.goto"),
35        ("c", "org.cycle"), ("C", "org.cycle-global"), ("s", "org.narrow.toggle"), ("k", "org.subtree.cut"),
36        ("U", "org.subtree.up"), ("D", "org.subtree.down"), ("r", "org.heading.demote"), ("l", "org.heading.promote"),
37        ("R", "org.subtree.demote"), ("L", "org.subtree.promote"), ("i", "org.heading.insert-after-subtree"),
38        ("^", "org.sort"), ("w", "app.refile"), ("a", "app.archive"), ("@", "org.subtree.mark"),
39        ("#", "org.heading.toggle-comment"), ("I", "app.clock.in"), ("O", "app.clock.out"), ("t", "org.todo.cycle"),
40        (",", "org.priority.set"), ("0", "org.priority.remove"), ("1", "org.priority.set-a"), ("2", "org.priority.set-b"),
41        ("3", "org.priority.set-c"), (":", "org.tags.set"), ("e", "org.effort.set"), ("v", "app.agenda"),
42        ("/", "org.sparse-tree"), ("o", "org.link.open"),
43    ]
44
45    static func speedBindings(mode: String?) -> String {
46        speedCommands.map { key, command in
47            "[[bind]]\nkeys = \"\(key)\"\ncommand = \"\(command)\"\nwhen = \"speed\"\n" + (mode.map { "mode = \"\($0)\"\n" } ?? "")
48        }.joined()
49    }
50
51    /// Doom: the Emacs preset's org bindings in every evil state (as org-mode-map's are), with
52    /// evil-org's `M-h/j/k/l` forms, then Doom's leader, localleader and evil-org keys.
53    static let common = """
54        [[bind]]
55        keys = "TAB"
56        command = "org.cycle"
57        when = "heading"
58        [[bind]]
59        keys = "TAB"
60        command = "org.cycle"
61        when = "fold-line"
62
63        [[bind]]
64        keys = "S-TAB"
65        command = "org.cycle-global"
66        [[bind]]
67        keys = "TAB"
68        command = "org.table.next-field"
69        when = "table"
70        [[bind]]
71        keys = "S-TAB"
72        command = "org.table.previous-field"
73        when = "table"
74        [[bind]]
75        keys = "RET"
76        command = "org.table.next-row"
77        when = "table"
78
79        """
80
81}