Sources/Orgstar/EmacsImportView.swift
99 lines · 3913 bytes
1import OrgApp
2import OrgCore
3import SwiftUI
4
5/// Import from Emacs: what was read from a configuration, to pick from before anything changes.
6struct EmacsImportView: View {
7 let workspace: WorkspaceModel?
8 @Environment(\.dismiss) private var dismiss
9 @State private var location: URL?
10 @State private var result: EmacsImport?
11 @State private var selected: Set<Int> = []
12 @State private var choosing = false
13 @State private var summary: String?
14 @State private var importing = false
15
16 var body: some View {
17 VStack(alignment: .leading, spacing: 0) {
18 header
19 Divider()
20 if let summary {
21 Text(summary).padding(20).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
22 } else if let result {
23 list(result)
24 } else {
25 ContentUnavailableView("No Emacs configuration found", systemImage: "doc.questionmark",
26 description: Text("Choose your init.el, or the folder holding it (~/.emacs.d, ~/.config/doom)."))
27 }
28 Divider()
29 HStack {
30 if let result, summary == nil {
31 Text(footnote(result)).font(.caption).foregroundStyle(.secondary)
32 }
33 Spacer()
34 if summary == nil {
35 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
36 Button("Import") { runImport() }
37 .keyboardShortcut(.defaultAction)
38 .disabled(selected.isEmpty || importing)
39 } else {
40 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
41 }
42 }
43 .padding(12)
44 }
45 .frame(width: 640, height: 560)
46 .onAppear {
47 if location == nil, let first = EmacsImport.defaultLocations().first { read(first) }
48 }
49 .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder, .item]) { chosen in
50 if case .success(let url) = chosen { read(url) }
51 }
52 }
53
54 private var header: some View {
55 HStack {
56 VStack(alignment: .leading, spacing: 2) {
57 Text("Import from Emacs").font(.headline)
58 Text(location.map { "Reading \($0.path)" + ((result?.isDoom ?? false) ? ", with Doom's own org defaults" : "") } ?? "")
59 .font(.caption).foregroundStyle(.secondary).lineLimit(2)
60 }
61 Spacer()
62 Button("Choose…") { choosing = true }
63 }
64 .padding(12)
65 }
66
67 private func list(_ result: EmacsImport) -> some View {
68 List {
69 EmacsImportSections(result: result, groups: result.groups(folders: true), selected: $selected,
70 font: .system(.body, design: .monospaced))
71 }
72 }
73
74 private func footnote(_ result: EmacsImport) -> String {
75 var parts = ["\(result.skipped) variables not about org left alone"]
76 if result.doomDefaultsLeftOut > 0 { parts.append("\(result.doomDefaultsLeftOut) Doom defaults orgstar has no use for") }
77 return parts.joined(separator: "; ")
78 }
79
80 private func read(_ url: URL) {
81 let accessing = url.startAccessingSecurityScopedResource()
82 defer { if accessing { url.stopAccessingSecurityScopedResource() } }
83 location = url
84 let scanned = EmacsImport.scan(EmacsImport.files(in: url))
85 result = scanned
86 selected = Set(scanned.items.map(\.id))
87 summary = nil
88 }
89
90 private func runImport() {
91 guard let result else { return }
92 importing = true
93 let chosen = result.items.filter { selected.contains($0.id) }
94 Task {
95 summary = await EmacsImport.apply(chosen, workspace: workspace)
96 importing = false
97 }
98 }
99}