import Foundation import OrgCore import OrgPresentation import OrgWorkspace /// The configuration folder (`$XDG_CONFIG_HOME/orgstar`, else `~/.config/orgstar`) and /// `config.toml` in it: every setting of the Settings window as text, to edit, keep in a /// dotfiles repository and share. The file is the source of truth: Settings writes its changes /// into it, keeping comments and order, and edits to it apply while the app runs. public enum UserConfig { /// A folder chosen in the app (on iOS, a synced copy of the Mac's), used over the default. nonisolated(unsafe) public static var directoryOverride: URL? public static var directory: URL { if let directoryOverride { return directoryOverride } let environment = ProcessInfo.processInfo.environment if let override = environment["ORGSTAR_CONFIG_DIR"] { return URL(fileURLWithPath: override) } // Development runs keep everything in their data folder. if let data = environment["ORGSTAR_DATA_DIR"] { return URL(fileURLWithPath: data) } if let xdg = environment["XDG_CONFIG_HOME"], !xdg.isEmpty { return URL(fileURLWithPath: xdg).appendingPathComponent("orgstar") } return URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent(".config/orgstar") } /// The theme `config.toml` (`text`) sets: its `[theme]` tables' colors over those of /// `theme-file` (a path relative to `folder`), over the palettes `light-theme` and /// `dark-theme` name; with problems in all three. Nil when `text` isn't TOML. public static func theme( config text: String, themeFile name: String?, lightTheme: String? = nil, darkTheme: String? = nil, in folder: URL, read: (URL) -> String? = { try? String(contentsOf: $0, encoding: .utf8) } ) -> (theme: ThemeSpec, problems: [String])? { guard let tables = try? TOML.parse(text) else { return nil } var (spec, problems) = ThemeSpec.base(light: lightTheme, dark: darkTheme) if let name, !name.isEmpty { if let themeText = read(folder.appendingPathComponent(name)) { do { let (read, more) = ThemeSpec.reading(try TOML.parse(themeText), over: spec, file: name) spec = read problems += more } catch { problems.append("\(name): \(error)") } } else { problems.append("config.toml: theme-file \(name) can't be read") } } let (merged, more) = ThemeSpec.reading(tables, over: spec) return (merged, problems + more) } /// The file Copy to Config Folder writes. public static let copiedThemeFile = "my-theme.toml" /// Writes the two palettes into `folder` as `copiedThemeFile`; false, writing nothing, when /// the file is already there. public static func copyTheme(light: Palette, dark: Palette, to folder: URL) throws -> Bool { let file = folder.appendingPathComponent(copiedThemeFile) guard !FileManager.default.fileExists(atPath: file.path) else { return false } try ThemeSpec.themeFile(light: light, dark: dark).write(to: file, atomically: true, encoding: .utf8) return true } /// Where earlier versions kept the configuration files. public static var legacyDirectory: URL { FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent("Orgstar") } /// A file of the configuration folder; one that exists only where earlier versions kept /// it (Application Support) is still read from there. public static func file(_ name: String) -> URL { let url = directory.appendingPathComponent(name) let legacy = legacyDirectory.appendingPathComponent(name) let manager = FileManager.default if !manager.fileExists(atPath: url.path), manager.fileExists(atPath: legacy.path) { return legacy } return url } public static var configFile: URL { directory.appendingPathComponent("config.toml") } /// One setting: where it sits in the file, its user-defaults key, and how values convert. /// Settings Emacs has a variable for sit at the top of the file under that variable's /// name; the rest go in `[orgstar]`. public struct Setting: Sendable { public enum Kind: Sendable { case bool, integer, string /// A string in the file for each stored value. case choice([(name: String, stored: String)]) case integerChoice([(name: String, stored: Int)]) /// `org-agenda-start-day`: `"-3d"` in the file, -3 stored. case dayOffset /// `display-line-numbers-type`: `true`, `false`, `"relative"` or `"visual"`, stored as /// a switch and, in `lineNumbersType`, the kind. case lineNumbers } public let section: String public let key: String public let defaultsKey: String public let kind: Kind public let fallback: TOML.Value public let comment: String /// Where earlier versions of the file kept it, `section.key`. public let legacy: String public var path: String { section.isEmpty ? key : section + "." + key } } public static let settings: [Setting] = [ Setting(section: "", key: "fill-column", defaultsKey: "fillColumn", kind: .integer, fallback: .integer(80), comment: "M-q fills to this column", legacy: "editor.fill-column"), Setting(section: "", key: "org-tags-column", defaultsKey: "tagsColumn", kind: .integer, fallback: .integer(-77), comment: "negative: tags end at that column; 0: one space before them", legacy: "editor.tags-column"), Setting(section: "", key: "org-insert-heading-respect-content", defaultsKey: "headingAfterSubtree", kind: .bool, fallback: .bool(false), comment: "M-RET adds the new heading after the subtree", legacy: "editor.insert-heading-respect-content"), Setting(section: "", key: "org-M-RET-may-split-line", defaultsKey: "splitLine", kind: .bool, fallback: .bool(false), comment: "M-RET splits the line at the caret", legacy: "editor.meta-return-may-split-line"), Setting(section: "", key: "org-use-property-inheritance", defaultsKey: "propertyInheritance", kind: .string, fallback: .string("nil"), comment: "nil, t, or property names that inherit, separated by spaces, such as \"STYLE\"", legacy: ""), Setting(section: "", key: "org-list-allow-alphabetical", defaultsKey: "alphabeticalLists", kind: .bool, fallback: .bool(true), comment: "lists can use a. b. c.", legacy: "editor.list-allow-alphabetical"), Setting(section: "", key: "org-hide-emphasis-markers", defaultsKey: "hiddenMarkers", kind: .bool, fallback: .bool(true), comment: "hide *bold* and =code= markers when markup is hidden", legacy: "editor.hide-emphasis-markers"), Setting(section: "", key: "org-pretty-entities", defaultsKey: "prettyEntities", kind: .bool, fallback: .bool(true), comment: "\\alpha shows as α and x_{1} is lowered when markup is hidden", legacy: "editor.pretty-entities"), Setting(section: "", key: "org-todo-keywords", defaultsKey: "todoKeywords", kind: .string, fallback: .string("TODO | DONE"), comment: "for files without #+TODO, in #+TODO syntax, one sequence per line (\\n); keys in parentheses turn on fast selection; read at launch", legacy: "editor.todo-keywords"), Setting(section: "", key: "org-log-done", defaultsKey: "logDone", kind: .choice([("nil", "nil"), ("time", "time"), ("note", "note")]), fallback: .string("nil"), comment: "nil, time (CLOSED when done) or note (CLOSED and a note)", legacy: ""), Setting(section: "", key: "org-log-reschedule", defaultsKey: "logReschedule", kind: .choice([("nil", "nil"), ("time", "time"), ("note", "note")]), fallback: .string("nil"), comment: "nil, time or note: log changing or removing a SCHEDULED date", legacy: ""), Setting(section: "", key: "org-log-redeadline", defaultsKey: "logRedeadline", kind: .choice([("nil", "nil"), ("time", "time"), ("note", "note")]), fallback: .string("nil"), comment: "nil, time or note: log changing or removing a DEADLINE", legacy: ""), Setting(section: "", key: "org-log-into-drawer", defaultsKey: "logIntoDrawer", kind: .string, fallback: .string(""), comment: "the drawer state notes go in (\"LOGBOOK\" is Emacs's t); \"\" for none", legacy: ""), Setting(section: "", key: "org-enforce-todo-dependencies", defaultsKey: "enforceTodoDependencies", kind: .bool, fallback: .bool(false), comment: "an entry can't be done before its TODO children, or under an ORDERED parent before its earlier siblings", legacy: ""), Setting(section: "", key: "org-enforce-todo-checkbox-dependencies", defaultsKey: "enforceTodoCheckboxDependencies", kind: .bool, fallback: .bool(false), comment: "an entry can't be done while it has unchecked boxes", legacy: ""), Setting(section: "", key: "org-archive-location", defaultsKey: "archiveLocation", kind: .string, fallback: .string("%s_archive::"), comment: "where Archive Subtree moves an entry without an ARCHIVE property or #+ARCHIVE: file::heading, %s the file's name", legacy: ""), Setting(section: "", key: "org-export-default-language", defaultsKey: "exportLanguage", kind: .string, fallback: .string("en"), comment: "the language of exports without #+LANGUAGE and of clock tables without :lang, such as \"de\"", legacy: ""), Setting(section: "", key: "org-use-tag-inheritance", defaultsKey: "tagInheritance", kind: .string, fallback: .string("t"), comment: "t, nil, tags that inherit separated by spaces, or a regexp in braces such as \"{^@}\"", legacy: ""), Setting(section: "", key: "org-tags-exclude-from-inheritance", defaultsKey: "tagsExcludedFromInheritance", kind: .string, fallback: .string(""), comment: "tags that never inherit, separated by spaces", legacy: ""), Setting(section: "", key: "org-tag-alist", defaultsKey: "tagAlist", kind: .string, fallback: .string(""), comment: "tags C-c C-q offers in files without #+TAGS, in #+TAGS syntax: \"work(w) home(h) { @office(o) @home(H) }\"", legacy: ""), Setting(section: "", key: "org-track-ordered-property-with-tag", defaultsKey: "trackOrderedTag", kind: .string, fallback: .string("nil"), comment: "nil, t (the tag ORDERED) or a tag that Toggle ORDERED Property toggles too", legacy: ""), Setting(section: "", key: "org-startup-indented", defaultsKey: "startupIndented", kind: .bool, fallback: .bool(true), comment: "indent bodies under their headings (org-indent-mode); #+STARTUP: (no)indent overrides", legacy: ""), Setting(section: "", key: "org-hide-leading-stars", defaultsKey: "hideLeadingStars", kind: .bool, fallback: .bool(false), comment: "show only a heading's last star without indentation; #+STARTUP: hidestars/showstars overrides", legacy: ""), Setting(section: "", key: "org-startup-align-all-tables", defaultsKey: "startupAlignAllTables", kind: .bool, fallback: .bool(false), comment: "align every table when a file opens; #+STARTUP: (no)align overrides", legacy: ""), Setting(section: "", key: "org-startup-truncated", defaultsKey: "startupTruncated", kind: .bool, fallback: .bool(false), comment: "truncate long lines instead of wrapping them (Emacs's default is t; Doom wraps)", legacy: ""), Setting(section: "", key: "spell-check", defaultsKey: "spellCheck", kind: .bool, fallback: .bool(false), comment: "check spelling while typing, outside code, links, dates, tags and keywords", legacy: ""), Setting(section: "", key: "electric-pair-mode", defaultsKey: "electricPair", kind: .bool, fallback: .bool(true), comment: "type brackets, <> and quotes in pairs as electric-pair-mode does (off in plain Emacs; Doom pairs with smartparens)", legacy: ""), Setting(section: "", key: "org-startup-with-inline-images", defaultsKey: "startupWithInlineImages", kind: .bool, fallback: .bool(false), comment: "show image links as images when a file opens; #+STARTUP: (no)inlineimages overrides", legacy: ""), Setting(section: "", key: "org-startup-with-latex-preview", defaultsKey: "startupWithLatexPreview", kind: .bool, fallback: .bool(false), comment: "show LaTeX fragments as typeset math when a file opens; #+STARTUP: (no)latexpreview overrides", legacy: ""), Setting(section: "", key: "org-roam-directory", defaultsKey: "roamDirectory", kind: .string, fallback: .string(""), comment: "the folder org-roam notes are in; empty for the first folder", legacy: ""), Setting(section: "", key: "org-roam-dailies-directory", defaultsKey: "dailiesDirectory", kind: .string, fallback: .string("daily/"), comment: "the folder daily notes are in, relative to org-roam-directory", legacy: ""), Setting(section: "", key: "org-roam-dailies-file", defaultsKey: "dailiesFile", kind: .string, fallback: .string("%<%Y-%m-%d>.org"), comment: "a daily note's file name; %<...> formats the date as format-time-string does", legacy: ""), Setting(section: "", key: "org-roam-dailies-head", defaultsKey: "dailiesHead", kind: .string, fallback: .string("#+title: %<%Y-%m-%d>\n"), comment: "what a new daily note starts with, under its ID", legacy: ""), Setting(section: "", key: "org-roam-dailies-entry", defaultsKey: "dailiesEntry", kind: .string, fallback: .string("* %?"), comment: "the capture template Capture to Daily Note files", legacy: ""), Setting(section: "", key: "auto-fill-mode", defaultsKey: "autoFill", kind: .bool, fallback: .bool(false), comment: "typing a space past fill-column breaks the line (View ▸ Auto Fill)", legacy: ""), Setting(section: "", key: "org-timer-format", defaultsKey: "timerFormat", kind: .string, fallback: .string("%s "), comment: "how Insert Timer Value writes the timer; %s is h:mm:ss", legacy: ""), Setting(section: "", key: "org-timer-default-timer", defaultsKey: "timerDefault", kind: .string, fallback: .string("0"), comment: "the countdown Set Countdown Timer offers, in minutes or h:mm:ss; 0 offers none", legacy: ""), Setting(section: "", key: "org-attach-id-dir", defaultsKey: "attachIDDir", kind: .string, fallback: .string("data/"), comment: "the folder attachment folders named by ID go in, relative to the org file's folder unless absolute", legacy: ""), Setting(section: "", key: "org-attach-use-inheritance", defaultsKey: "attachUseInheritance", kind: .choice([("selective", "selective"), ("t", "t"), ("nil", "nil")]), fallback: .string("selective"), comment: "whether DIR and ID inherit for attachments: selective (as org-use-property-inheritance), t or nil", legacy: ""), Setting(section: "", key: "org-yank-image-save-method", defaultsKey: "yankImageSaveMethod", kind: .string, fallback: .string("attach"), comment: "where pasted images go: attach (the entry's attachments) or a folder, relative to the org file's folder unless absolute", legacy: ""), Setting(section: "", key: "org-yank-dnd-method", defaultsKey: "yankDndMethod", kind: .choice([("ask", "ask"), ("attach", "attach"), ("open", "open"), ("file-link", "file-link")]), fallback: .string("ask"), comment: "files dropped on the editor: ask, attach, open or file-link", legacy: ""), Setting(section: "", key: "org-crypt-tag-matcher", defaultsKey: "cryptTagMatcher", kind: .string, fallback: .string("crypt"), comment: "the tag of entries Encrypt Tagged Entries encrypts", legacy: ""), Setting(section: "", key: "org-display-remote-inline-images", defaultsKey: "remoteInlineImages", kind: .choice([("skip", "skip"), ("download", "download"), ("cache", "cache")]), fallback: .string("skip"), comment: "images at http(s) links: skip, download (each run) or cache (kept on disk)", legacy: ""), Setting(section: "", key: "org-use-speed-commands", defaultsKey: "useSpeedCommands", kind: .bool, fallback: .bool(false), comment: "single keys at the start of a heading line run commands (n, p, t, c, …)", legacy: ""), Setting(section: "", key: "org-cycle-hide-drawer-startup", defaultsKey: "hideDrawerStartup", kind: .bool, fallback: .bool(true), comment: "fold drawers when a file opens; #+STARTUP: nohidedrawers overrides", legacy: ""), Setting(section: "", key: "org-cycle-hide-block-startup", defaultsKey: "hideBlockStartup", kind: .bool, fallback: .bool(false), comment: "fold blocks when a file opens; #+STARTUP: hideblocks overrides", legacy: ""), Setting(section: "", key: "display-line-numbers-type", defaultsKey: "showLineNumbers", kind: .lineNumbers, fallback: .bool(false), comment: "line numbers: true, false, \"relative\" or \"visual\" (View ▸ Line Numbers)", legacy: "editor.line-numbers"), Setting(section: "", key: "org-agenda-span", defaultsKey: "agendaSpan", kind: .integer, fallback: .integer(10), comment: "days the agenda shows", legacy: "agenda.span"), Setting(section: "", key: "org-agenda-start-day", defaultsKey: "agendaStartOffset", kind: .dayOffset, fallback: .string("+0d"), comment: "the agenda's first day: \"-3d\", \"+0d\"", legacy: "agenda.start-offset"), Setting(section: "", key: "org-deadline-warning-days", defaultsKey: "deadlineWarningDays", kind: .integer, fallback: .integer(14), comment: "days ahead the agenda warns about a deadline; a -Nd cookie overrides", legacy: ""), Setting(section: "", key: "org-agenda-start-on-weekday", defaultsKey: "agendaStartOnWeekday", kind: .choice([("nil", "nil")] + (0...6).map { ("\($0)", "\($0)") }), fallback: .string("1"), comment: "weeks start on this day, 0 Sunday to 6 Saturday, or nil for the agenda's first day", legacy: ""), Setting(section: "", key: "org-agenda-show-all-dates", defaultsKey: "agendaShowAllDates", kind: .bool, fallback: .bool(false), comment: "show days with no entries (Emacs's default is t; orgstar hides them); today always shows", legacy: ""), Setting(section: "", key: "org-agenda-skip-scheduled-if-done", defaultsKey: "agendaSkipScheduledIfDone", kind: .bool, fallback: .bool(false), comment: "leave done entries out of the agenda even on the day they are scheduled", legacy: ""), Setting(section: "", key: "org-agenda-skip-deadline-if-done", defaultsKey: "agendaSkipDeadlineIfDone", kind: .bool, fallback: .bool(false), comment: "leave done entries out of the agenda even on the day they are due", legacy: ""), Setting(section: "", key: "org-habit-show-habits-only-for-today", defaultsKey: "habitsOnlyForToday", kind: .bool, fallback: .bool(true), comment: "habits show only on today's agenda, not on the later days they are scheduled for", legacy: ""), Setting(section: "", key: "org-agenda-log-mode-items", defaultsKey: "agendaLogModeItems", kind: .string, fallback: .string("closed clock"), comment: "what log mode shows, separated by spaces: closed, clock, state", legacy: ""), Setting(section: "", key: "org-agenda-todo-ignore-with-date", defaultsKey: "agendaTodoIgnoreWithDate", kind: .bool, fallback: .bool(false), comment: "the TODO list leaves out entries with any active timestamp", legacy: ""), Setting(section: "", key: "org-agenda-todo-ignore-scheduled", defaultsKey: "agendaTodoIgnoreScheduled", kind: .string, fallback: .string("nil"), comment: "the TODO list leaves out scheduled entries: nil, all, future, past, or days ahead (negative: days ago)", legacy: ""), Setting(section: "", key: "org-agenda-todo-ignore-deadlines", defaultsKey: "agendaTodoIgnoreDeadlines", kind: .string, fallback: .string("nil"), comment: "the TODO list leaves out entries with deadlines: nil, near, far, all, future, past, or days", legacy: ""), Setting(section: "", key: "org-agenda-todo-ignore-timestamp", defaultsKey: "agendaTodoIgnoreTimestamp", kind: .string, fallback: .string("nil"), comment: "the TODO list leaves out entries with other active timestamps: nil, all, future, past, or days", legacy: ""), Setting(section: "", key: "org-agenda-tags-todo-honor-ignore-options", defaultsKey: "agendaTagsTodoHonorIgnore", kind: .bool, fallback: .bool(false), comment: "the org-agenda-todo-ignore settings apply to TODO-only matches too", legacy: ""), Setting(section: "", key: "org-agenda-search-view-always-boolean", defaultsKey: "agendaSearchAlwaysBoolean", kind: .bool, fallback: .bool(false), comment: "search strings are always +word -word {regexp} snippets, never a phrase", legacy: ""), Setting(section: "", key: "org-agenda-search-view-force-full-words", defaultsKey: "agendaSearchForceFullWords", kind: .bool, fallback: .bool(false), comment: "search snippets match whole words only", legacy: ""), Setting(section: "", key: "org-agenda-dim-blocked-tasks", defaultsKey: "agendaDimBlockedTasks", kind: .choice([("t", "t"), ("nil", "nil"), ("invisible", "invisible")]), fallback: .string("t"), comment: "t dims entries the enforce settings block, invisible hides them (unless only checkboxes block them), nil shows them as usual", legacy: ""), Setting(section: "", key: "appt-message-warning-time", defaultsKey: "reminderLead", kind: .integer, fallback: .integer(12), comment: "minutes of warning before timed entries; APPT_WARNTIME overrides", legacy: "agenda.reminder-lead"), Setting(section: "", key: "org-clock-idle-time", defaultsKey: "clockIdleTime", kind: .integer, fallback: .integer(0), comment: "minutes idle with a clock running before asking what to do with the time; 0 never (nil)", legacy: ""), Setting(section: "", key: "org-clock-into-drawer", defaultsKey: "clockIntoDrawer", kind: .string, fallback: .string("t"), comment: "t: clock lines go in the log drawer (LOGBOOK); nil: none; a number: once there are that many; or a drawer name", legacy: ""), Setting(section: "", key: "org-clock-rounding-minutes", defaultsKey: "clockRoundingMinutes", kind: .integer, fallback: .integer(0), comment: "clocks start and end on multiples of this many minutes; 0 doesn't round", legacy: ""), Setting(section: "", key: "org-clock-out-remove-zero-time-clocks", defaultsKey: "clockRemoveZeroTime", kind: .bool, fallback: .bool(false), comment: "clocking out removes a clock line of 0:00", legacy: ""), Setting(section: "", key: "org-clock-in-switch-to-state", defaultsKey: "clockInSwitchToState", kind: .string, fallback: .string(""), comment: "the TODO keyword an entry takes when you clock in; \"\" leaves it", legacy: ""), Setting(section: "", key: "org-clock-out-when-done", defaultsKey: "clockOutWhenDone", kind: .string, fallback: .string("t"), comment: "t: marking the clocked entry done clocks out; nil: it doesn't; or keywords that clock out, separated by spaces", legacy: ""), Setting(section: "", key: "org-clock-history-length", defaultsKey: "clockHistoryLength", kind: .integer, fallback: .integer(5), comment: "recently clocked entries to remember", legacy: ""), Setting(section: "", key: "org-refile-targets", defaultsKey: "refileTargets", kind: .string, fallback: .string(""), comment: "as Lisp: ((nil :maxlevel . 3) (org-agenda-files :tag . \"project\")); \"\" offers headings to level 3 in every file", legacy: ""), Setting(section: "", key: "org-refile-use-outline-path", defaultsKey: "refileUseOutlinePath", kind: .string, fallback: .string("file"), comment: "how targets are named: nil, t, file, full-file-path, buffer-name or title", legacy: ""), Setting(section: "", key: "org-refile-allow-creating-parent-nodes", defaultsKey: "refileAllowCreatingParentNodes", kind: .string, fallback: .string("nil"), comment: "a target typed as parent/new adds the heading: t, confirm (asks first) or nil", legacy: ""), Setting(section: "", key: "org-agenda-files", defaultsKey: "agendaFiles", kind: .string, fallback: .string(""), comment: "files and folders the agenda reads, separated by \\n; a file that isn't .org lists more, one per line; \"\" reads the folders' files", legacy: ""), Setting(section: "", key: "org-icalendar-use-deadline", defaultsKey: "icalendarUseDeadline", kind: .string, fallback: .string("event-if-not-todo todo-due"), comment: "iCalendar export: what a DEADLINE makes, of event-if-not-todo, event-if-todo, event-if-todo-not-done and todo-due", legacy: ""), Setting(section: "", key: "org-icalendar-use-scheduled", defaultsKey: "icalendarUseScheduled", kind: .string, fallback: .string("todo-start"), comment: "iCalendar export: what SCHEDULED makes, of event-if-not-todo, event-if-todo, event-if-todo-not-done and todo-start", legacy: ""), Setting(section: "", key: "org-icalendar-include-todo", defaultsKey: "icalendarIncludeTodo", kind: .string, fallback: .string("nil"), comment: "iCalendar export: tasks as VTODOs: nil, t (not done), all, unblocked, or TODO keywords separated by spaces", legacy: ""), Setting(section: "", key: "org-icalendar-combined-agenda-file", defaultsKey: "icalendarCombinedAgendaFile", kind: .string, fallback: .string("~/org.ics"), comment: "where Combine Agenda Files into iCalendar writes", legacy: ""), Setting(section: "", key: "org-agenda-use-time-grid", defaultsKey: "agendaUseTimeGrid", kind: .bool, fallback: .bool(true), comment: "time grid lines among timed entries, as [org-agenda-time-grid] says", legacy: ""), Setting(section: "", key: "org-agenda-show-current-time-in-grid", defaultsKey: "agendaShowCurrentTime", kind: .bool, fallback: .bool(true), comment: "the now line on today's grid", legacy: ""), Setting(section: "", key: "org-agenda-sort-notime-is-late", defaultsKey: "agendaSortNotimeIsLate", kind: .bool, fallback: .bool(true), comment: "lines without a time sort after timed ones for time-up, and without a date after dated ones", legacy: ""), Setting(section: "", key: "org-agenda-sort-noeffort-is-high", defaultsKey: "agendaSortNoeffortIsHigh", kind: .bool, fallback: .bool(true), comment: "entries without an effort sort as the longest for effort-up", legacy: ""), Setting(section: "org-agenda-prefix-format", key: "agenda", defaultsKey: "agendaPrefixAgenda", kind: .string, fallback: .string(PrefixFormat.defaults["agenda"]!), comment: "%c category, %t time, %s scheduled/deadline, %e effort, %l level, %b outline path; %-12 pads", legacy: ""), Setting(section: "org-agenda-prefix-format", key: "todo", defaultsKey: "agendaPrefixTodo", kind: .string, fallback: .string(PrefixFormat.defaults["todo"]!), comment: "the TODO list", legacy: ""), Setting(section: "org-agenda-prefix-format", key: "tags", defaultsKey: "agendaPrefixTags", kind: .string, fallback: .string(PrefixFormat.defaults["tags"]!), comment: "tag and property matches", legacy: ""), Setting(section: "org-agenda-sorting-strategy", key: "agenda", defaultsKey: "agendaSortAgenda", kind: .string, fallback: .string(AgendaSort.defaults["agenda"]!.joined(separator: " ")), comment: "how lines of a day are ordered: time-up, priority-down, category-keep, alpha-up, effort-up, tag-up, todo-state-up, …", legacy: ""), Setting(section: "org-agenda-sorting-strategy", key: "todo", defaultsKey: "agendaSortTodo", kind: .string, fallback: .string(AgendaSort.defaults["todo"]!.joined(separator: " ")), comment: "the TODO list", legacy: ""), Setting(section: "org-agenda-sorting-strategy", key: "tags", defaultsKey: "agendaSortTags", kind: .string, fallback: .string(AgendaSort.defaults["tags"]!.joined(separator: " ")), comment: "tag and property matches", legacy: ""), Setting(section: "org-agenda-sorting-strategy", key: "search", defaultsKey: "agendaSortSearch", kind: .string, fallback: .string(AgendaSort.defaults["search"]!.joined(separator: " ")), comment: "text search", legacy: ""), Setting(section: "org-stuck-projects", key: "match", defaultsKey: "stuckProjectsMatch", kind: .string, fallback: .string("+LEVEL=2/-DONE"), comment: "the entries that are projects, as a tag and property match", legacy: ""), Setting(section: "org-stuck-projects", key: "todo", defaultsKey: "stuckProjectsTodo", kind: .string, fallback: .string("TODO NEXT NEXTACTION"), comment: "a heading in the project with one of these keywords unsticks it; * for any open keyword", legacy: ""), Setting(section: "org-stuck-projects", key: "tags", defaultsKey: "stuckProjectsTags", kind: .string, fallback: .string(""), comment: "a heading in the project with one of these tags unsticks it; * for any tag", legacy: ""), Setting(section: "org-stuck-projects", key: "regexp", defaultsKey: "stuckProjectsRegexp", kind: .string, fallback: .string(""), comment: "text in the project matching this Emacs regexp unsticks it", legacy: ""), Setting(section: "org-agenda-time-grid", key: "when", defaultsKey: "agendaTimeGridWhen", kind: .string, fallback: .string("daily today require-timed"), comment: "daily (one-day views), today, weekly (every day), require-timed (only with timed entries), remove-match (no line at an entry's time)", legacy: ""), Setting(section: "org-agenda-time-grid", key: "times", defaultsKey: "agendaTimeGridTimes", kind: .string, fallback: .string("800 1000 1200 1400 1600 1800 2000"), comment: "the grid's times as HHMM, separated by spaces", legacy: ""), Setting(section: "theme", key: "font", defaultsKey: "themeFont", kind: .string, fallback: .string(""), comment: "a monospaced font family; \"\" for the system's", legacy: ""), Setting(section: "theme", key: "body-font", defaultsKey: "themeBodyFont", kind: .string, fallback: .string(""), comment: "a font family for prose, or \"system\"; tables, code, dates and tags keep the font above; \"\" for the font above throughout", legacy: ""), Setting(section: "theme", key: "font-size", defaultsKey: "themeFontSize", kind: .integer, fallback: .integer(13), comment: "points", legacy: ""), Setting(section: "theme", key: "line-spacing", defaultsKey: "themeLineSpacing", kind: .integer, fallback: .integer(2), comment: "points between lines", legacy: ""), Setting(section: "theme", key: "text-width", defaultsKey: "themeTextWidth", kind: .integer, fallback: .integer(0), comment: "the widest the text runs, in columns, centred in a wider editor; 0 for the whole width", legacy: ""), Setting(section: "theme", key: "heading-size-step", defaultsKey: "themeHeadingStep", kind: .integer, fallback: .integer(1), comment: "points a heading is larger than the level below; level 4 and deeper are body size", legacy: ""), Setting(section: "theme", key: "theme-file", defaultsKey: "themeFile", kind: .string, fallback: .string(""), comment: "a theme in its own file in this folder, under the colors set here; colors: see default-theme.toml", legacy: ""), Setting(section: "theme", key: "light-theme", defaultsKey: "themeLight", kind: .string, fallback: .string("default-light"), comment: "the built-in theme for the light appearance: default-light, solarized-light, gruvbox-light, modus-operandi, catppuccin-latte, tomorrow or paper", legacy: ""), Setting(section: "theme", key: "dark-theme", defaultsKey: "themeDark", kind: .string, fallback: .string("default-dark"), comment: "the built-in theme for the dark appearance: default-dark, solarized-dark, gruvbox-dark, modus-vivendi, catppuccin-mocha, nord, dracula, tokyo-night or doom-one", legacy: ""), Setting(section: "orgstar", key: "save", defaultsKey: "saveMode", kind: .choice([("automatic", "automatic"), ("explicit", "explicit")]), fallback: .string("automatic"), comment: "automatic (1 s after typing stops) or explicit (only with ⌘S)", legacy: "general.save"), Setting(section: "orgstar", key: "keymap", defaultsKey: "keymap", kind: .choice([("emacs", "emacs"), ("mac", "mac"), ("doom", "doom")]), fallback: .string("emacs"), comment: "emacs, mac or doom; your own bindings go in keymap.toml", legacy: "general.keymap"), Setting(section: "orgstar", key: "option-as-meta", defaultsKey: "optionAsMeta", kind: .integerChoice([("left", 1), ("right", 2), ("both", 3), ("none", 0)]), fallback: .string("left"), comment: "left, right, both or none", legacy: "general.option-as-meta"), Setting(section: "orgstar", key: "pdf-margin", defaultsKey: "pdfMargin", kind: .integer, fallback: .integer(50), comment: "PDF export's page margins, in points", legacy: ""), Setting(section: "orgstar", key: "pdf-font", defaultsKey: "pdfFont", kind: .string, fallback: .string(""), comment: "a font family for PDF export's text; \"\" for the export's own", legacy: ""), Setting(section: "orgstar", key: "pdf-page-numbers", defaultsKey: "pdfPageNumbers", kind: .bool, fallback: .bool(true), comment: "number PDF export's pages", legacy: ""), Setting(section: "orgstar", key: "tab-bar", defaultsKey: "showTabBar", kind: .bool, fallback: .bool(false), comment: "a tab for each open buffer above the editor (View ▸ Show Tab Bar)", legacy: ""), Setting(section: "orgstar", key: "show-markup", defaultsKey: "showMarkup", kind: .bool, fallback: .bool(false), comment: "show link brackets and emphasis markers (View ▸ Show Markup)", legacy: "editor.show-markup"), Setting(section: "orgstar", key: "show-hidden-files", defaultsKey: "showHiddenFiles", kind: .bool, fallback: .bool(true), comment: "list dotfiles and dot folders in your folders", legacy: ""), Setting(section: "orgstar", key: "ignored-folders", defaultsKey: "ignoredFolders", kind: .string, fallback: .string(ScanRules.defaultIgnoredFolders.sorted().joined(separator: " ")), comment: "folder names never listed or searched, separated by spaces", legacy: ""), Setting(section: "orgstar", key: "agenda-include-subfolders", defaultsKey: "agendaSubfolders", kind: .bool, fallback: .bool(false), comment: "the agenda reads org files in subfolders too", legacy: "agenda.include-subfolders"), Setting(section: "orgstar", key: "reminders", defaultsKey: "reminders", kind: .bool, fallback: .bool(true), comment: "notify before timed entries", legacy: "agenda.reminders"), Setting(section: "orgstar", key: "calendar-events", defaultsKey: "calendarEvents", kind: .bool, fallback: .bool(false), comment: "show events from Calendar in the agenda", legacy: ""), Setting(section: "orgstar", key: "calendar-event-calendars", defaultsKey: "calendarEventCalendars", kind: .string, fallback: .string(""), comment: "the calendars whose events show, by title or identifier, separated by commas; \"\" for all", legacy: ""), Setting(section: "orgstar", key: "agenda-reminders", defaultsKey: "agendaReminders", kind: .bool, fallback: .bool(false), comment: "show incomplete reminders with a due date from Reminders in the agenda, read-only", legacy: ""), Setting(section: "orgstar", key: "agenda-reminder-lists", defaultsKey: "agendaReminderLists", kind: .string, fallback: .string(""), comment: "the Reminders lists shown, by title or identifier, separated by commas; \"\" for all", legacy: ""), Setting(section: "orgstar", key: "global-capture-hotkey", defaultsKey: "globalCapture", kind: .bool, fallback: .bool(true), comment: "⌃⌥Space opens Capture from any app; templates go in capture.toml", legacy: "capture.global-hotkey"), ] /// `"-3d"` as -3; nil for anything else. static func dayOffset(_ s: String) -> Int? { guard s.hasSuffix("d"), let n = Int(s.dropLast().replacingOccurrences(of: "+", with: "")) else { return nil } return n } // MARK: - Values /// The file value for what `defaults` holds, or the fallback. public static func value(_ setting: Setting, in defaults: UserDefaults) -> TOML.Value { guard defaults.object(forKey: setting.defaultsKey) != nil else { return setting.fallback } switch setting.kind { case .bool: return .bool(defaults.bool(forKey: setting.defaultsKey)) case .integer: return .integer(defaults.integer(forKey: setting.defaultsKey)) case .string: return .string(defaults.string(forKey: setting.defaultsKey) ?? "") case .choice(let choices): let stored = defaults.string(forKey: setting.defaultsKey) return choices.first { $0.stored == stored }.map { .string($0.name) } ?? setting.fallback case .integerChoice(let choices): let stored = defaults.integer(forKey: setting.defaultsKey) return choices.first { $0.stored == stored }.map { .string($0.name) } ?? setting.fallback case .dayOffset: let days = defaults.integer(forKey: setting.defaultsKey) return .string((days < 0 ? "" : "+") + "\(days)d") case .lineNumbers: guard defaults.bool(forKey: setting.defaultsKey) else { return .bool(false) } let type = defaults.string(forKey: "lineNumbersType") ?? "" return ["relative", "visual"].contains(type) ? .string(type) : .bool(true) } } /// Stores a file value; a value of the wrong kind is a problem. static func store(_ value: TOML.Value, _ setting: Setting, in defaults: UserDefaults) -> String? { let key = setting.defaultsKey switch (setting.kind, value) { case (.lineNumbers, .bool(let b)): defaults.set(b, forKey: key) defaults.set("t", forKey: "lineNumbersType") case (.lineNumbers, .string(let s)) where ["relative", "visual", "t", "nil"].contains(s): defaults.set(s != "nil", forKey: key) defaults.set(s == "nil" ? "t" : s, forKey: "lineNumbersType") case (.bool, .bool(let b)): defaults.set(b, forKey: key) case (.integer, .integer(let i)): defaults.set(i, forKey: key) case (.string, .string(let s)): defaults.set(s, forKey: key) case (.choice(let choices), .string(let s)): guard let choice = choices.first(where: { $0.name == s }) else { return "\(setting.key) must be one of \(choices.map(\.name).joined(separator: ", "))" } defaults.set(choice.stored, forKey: key) case (.integerChoice(let choices), .string(let s)): guard let choice = choices.first(where: { $0.name == s }) else { return "\(setting.key) must be one of \(choices.map(\.name).joined(separator: ", "))" } defaults.set(choice.stored, forKey: key) case (.dayOffset, .string(let s)): guard let days = dayOffset(s) else { return "\(setting.key) must look like \"-3d\" or \"+0d\"" } defaults.set(days, forKey: key) default: return "\(setting.path) has the wrong kind of value" } return nil } /// Applies a file to `defaults`; settings it leaves out go back to their defaults. /// Returns problems. @discardableResult public static func apply(_ text: String, to defaults: UserDefaults) -> [String] { let tables: [TOML.Table] do { tables = try TOML.parse(text) } catch { return ["config.toml: \(error)"] } var problems: [String] = [] let present = Set(tables.flatMap { table in table.values.keys.map { path(table.name, $0) } }) for setting in settings where !present.contains(setting.path) && !present.contains(setting.legacy) { if defaults.object(forKey: setting.defaultsKey) != nil { defaults.removeObject(forKey: setting.defaultsKey) } } let templates = tables.first { $0.name == structureTemplateTable } if templates == nil, defaults.object(forKey: structureTemplatesKey) != nil { defaults.removeObject(forKey: structureTemplatesKey) } for table in tables { if table.name == structureTemplateTable { var rows: [String] = [] for key in table.keys { guard let type = table.values[key]?.string, !type.isEmpty else { problems.append("config.toml: \(structureTemplateTable).\(key) must be a block type, such as \"src python\"") continue } rows.append(key + "\t" + type) } if defaults.stringArray(forKey: structureTemplatesKey) != rows { defaults.set(rows, forKey: structureTemplatesKey) } continue } // Colors are the theme's to read; publishing projects are `Publish`'s. if ThemeSpec.isThemeTable(table.name), table.name != "theme" { continue } if table.name == "publish", table.isArrayElement { continue } for (key, value) in table.values.sorted(by: { $0.key < $1.key }) { if table.name == "theme", !ThemeSpec.typeKeys.contains(key) { continue } let name = path(table.name, key) guard let setting = settings.first(where: { $0.path == name || $0.legacy == name }) else { problems.append("config.toml: unknown setting \(name)") continue } if let current = currentValue(setting, in: defaults), current == value { continue } if let problem = store(value, setting, in: defaults) { problems.append("config.toml: \(problem)") } } } return problems } /// `[org-structure-template-alist]`: block templates by key, over Org's, as /// `key = "type"`. Kept in user defaults under `structureTemplatesKey` as `keytype`. public static let structureTemplateTable = "org-structure-template-alist" static let structureTemplatesKey = "structureTemplates" /// The templates of `[org-structure-template-alist]`. public static func structureTemplates(_ defaults: UserDefaults) -> [StructureTemplate] { (defaults.stringArray(forKey: structureTemplatesKey) ?? []).compactMap { row in guard let tab = row.firstIndex(of: "\t") else { return nil } return StructureTemplate(key: String(row[.. String { section.isEmpty ? key : section + "." + key } /// Whether the file uses names from before settings took Emacs's names. public static func usesLegacyNames(_ text: String) -> Bool { let names = Set(settings.map(\.legacy)) return values(in: text).keys.contains { names.contains($0) } } /// `text` with settings under their old names moved to their current names and places, /// keeping comments and everything else. A section the move leaves empty is removed. public static func migratingLegacyNames(_ text: String) -> String { let values = values(in: text) var blocks: [[String]] = [[]] for line in text.components(separatedBy: "\n") { if line.trimmingCharacters(in: .whitespaces).hasPrefix("[") { blocks.append([]) } blocks[blocks.count - 1].append(line) } var moves: [(setting: Setting, value: TOML.Value, comment: String)] = [] var kept: [String] = [] for block in blocks { let header = block.first.map { $0.trimmingCharacters(in: .whitespaces) }.flatMap { $0.hasPrefix("[") ? $0 : nil } let section = header.map { String($0.dropFirst().prefix { $0 != "]" }).trimmingCharacters(in: .whitespaces) } ?? "" var remaining: [String] = [] for (index, line) in block.enumerated() { guard header == nil || index > 0, let equals = line.firstIndex(of: "=") else { remaining.append(line) continue } let name = path(section, line[.. TOML.Value? { defaults.object(forKey: setting.defaultsKey) == nil ? nil : value(setting, in: defaults) } /// The values a file sets, by `section.key`. static func values(in text: String) -> [String: TOML.Value] { guard let tables = try? TOML.parse(text) else { return [:] } var result: [String: TOML.Value] = [:] for table in tables { for (key, value) in table.values { result[path(table.name, key)] = value } } return result } // MARK: - Text static func format(_ value: TOML.Value) -> String { switch value { case .bool(let b): b ? "true" : "false" case .integer(let i): String(i) case .string(let s): "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") .replacingOccurrences(of: "\n", with: "\\n").replacingOccurrences(of: "\t", with: "\\t") + "\"" } } /// A commented file with every setting at its current value. public static func template(from defaults: UserDefaults) -> String { template { value($0, in: defaults) } } /// A commented file with every setting at its default. public static var defaultTemplate: String { template(\.fallback) } private static func template(_ value: (Setting) -> TOML.Value) -> String { var out = """ # orgstar settings. The Settings window writes its changes here, and edits made here # apply while orgstar runs. Keys go in keymap.toml, capture templates in capture.toml # and saved agenda views in views.toml, all in this folder. # Emacs variables, by their Emacs names. """ var section = "" for setting in settings { if setting.section != section { section = setting.section out += "\n[\(section)]\n" } out += "\(setting.key) = \(format(value(setting))) # \(setting.comment)\n" } return out } /// `text` with the sections it lacks entirely added at the end, every setting at its /// value in `defaults`: how a file from an earlier version gets settings added since. /// Single lines left out stay out; they mean the default. public static func addingMissingSections(_ text: String, from defaults: UserDefaults) -> String { let tables = (try? TOML.parse(text)) ?? [] let present = Set(tables.filter { !$0.values.isEmpty || $0.name != "" }.map(\.name)) var out = text var section: String? for setting in settings where !setting.section.isEmpty && !present.contains(setting.section) { if setting.section != section { section = setting.section if !out.hasSuffix("\n") { out += "\n" } out += "\n[\(setting.section)]\n" } out += "\(setting.key) = \(format(value(setting, in: defaults))) # \(setting.comment)\n" } return out } /// `text` with `section.key` set to `value`: the line's value is replaced (its comment /// stays), or a line is added at the end of the section, or the section at the end. public static func setting(_ text: String, section: String, key: String, to value: TOML.Value) -> String { var lines = text.components(separatedBy: "\n") var current = "" var lastInSection: Int? for (index, line) in lines.enumerated() { let trimmed = line.trimmingCharacters(in: .whitespaces) if trimmed.hasPrefix("[") { // An array of tables, such as `[[publish]]`, is no section a setting is in. current = trimmed.hasPrefix("[[") ? trimmed : String(trimmed.dropFirst().prefix { $0 != "]" }).trimmingCharacters(in: .whitespaces) if current == section { lastInSection = index } continue } guard current == section else { continue } if !trimmed.isEmpty { lastInSection = index } guard let equals = line.firstIndex(of: "="), line[.. String.Index? { var quote: Character? var index = line.index(after: equals) while index < line.endIndex { let c = line[index] if let q = quote { if c == "\\" && q == "\"" { index = line.index(after: index) } else if c == q { quote = nil } } else if c == "\"" || c == "'" { quote = c } else if c == "#" { return index } if index < line.endIndex { index = line.index(after: index) } } return nil } }