krz/orgstar

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

Sources/OrgApp/KeymapLoader.swift

85c633a83a4744bef9dde8b52462f9c2a1c037e4
orgstar/Sources/OrgApp/KeymapLoader.swift history · blame · raw

21 lines · 893 bytes

 1import Foundation
 2import OrgCore
 3
 4@MainActor
 5public enum KeymapLoader {
 6    /// `<data dir>/keymap.toml`.
 7    public static var userFile: URL { WorkspaceModel.defaultDirectory.appendingPathComponent("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.
11    public static func load(_ preset: KeymapPreset, userFile: URL = userFile) -> (keymap: Keymap, problems: [String]) {
12        guard let text = try? String(contentsOf: userFile, encoding: .utf8) else { return (preset.keymap, []) }
13        var problems: [String] = []
14        do {
15            let user = try Keymap(toml: text, problems: &problems)
16            return (.layered([preset.keymap, user]), problems)
17        } catch {
18            return (preset.keymap, ["\(error)"])
19        }
20    }
21}