krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Sources/OrgApp/KeymapLoader.swift

778058aafc7ddacee4a613b1da87c60aa0c8e113
orgstar/Sources/OrgApp/KeymapLoader.swift history · blame · raw

22 lines · 985 bytes

 1import Foundation
 2import OrgCore
 3
 4@MainActor
 5public enum KeymapLoader {
 6    /// `<data dir>/keymap.toml`.
 7    public static var userFile: URL { UserConfig.file("keymap.toml") }
 8
 9    /// The preset with the user's file on top, and the problems found in the file. A missing
10    /// file adds nothing; a file that isn't valid TOML adds nothing and is reported. With
11    /// `commands`, bindings of other command ids are reported and skipped.
12    public static func load(_ preset: KeymapPreset, userFile: URL = userFile, commands: Set<String>? = nil) -> (keymap: Keymap, problems: [String]) {
13        guard let text = try? String(contentsOf: userFile, encoding: .utf8) else { return (preset.keymap, []) }
14        var problems: [String] = []
15        do {
16            let user = try Keymap(toml: text, problems: &problems, commands: commands)
17            return (.layered([preset.keymap, user]), problems)
18        } catch {
19            return (preset.keymap, ["\(error)"])
20        }
21    }
22}