Sources/Orgstar/EmacsImportView.swift

213799f3f0792831bdbd707d1f85cb19b614f39f
orgstar/Sources/Orgstar/EmacsImportView.swift

104 lines · 4131 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            // The sandbox can't look in the home folder, so the App Store build starts with the picker.
 48            #if APPSTORE
 49            if location == nil { choosing = true }
 50            #else
 51            if location == nil, let first = EmacsImport.defaultLocations().first { read(first) }
 52            #endif
 53        }
 54        .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder, .item]) { chosen in
 55            if case .success(let url) = chosen { read(url) }
 56        }
 57    }
 58
 59    private var header: some View {
 60        HStack {
 61            VStack(alignment: .leading, spacing: 2) {
 62                Text("Import from Emacs").font(.headline)
 63                Text(location.map { "Reading \($0.path)" + ((result?.isDoom ?? false) ? ", with Doom's own org defaults" : "") } ?? "")
 64                    .font(.caption).foregroundStyle(.secondary).lineLimit(2)
 65            }
 66            Spacer()
 67            Button("Choose…") { choosing = true }
 68        }
 69        .padding(12)
 70    }
 71
 72    private func list(_ result: EmacsImport) -> some View {
 73        List {
 74            EmacsImportSections(result: result, groups: result.groups(folders: true), selected: $selected,
 75                                font: .system(.body, design: .monospaced))
 76        }
 77    }
 78
 79    private func footnote(_ result: EmacsImport) -> String {
 80        var parts = ["\(result.skipped) variables not about org left alone"]
 81        if result.doomDefaultsLeftOut > 0 { parts.append("\(result.doomDefaultsLeftOut) Doom defaults orgstar has no use for") }
 82        return parts.joined(separator: "; ")
 83    }
 84
 85    private func read(_ url: URL) {
 86        let accessing = url.startAccessingSecurityScopedResource()
 87        defer { if accessing { url.stopAccessingSecurityScopedResource() } }
 88        location = url
 89        let scanned = EmacsImport.scan(EmacsImport.files(in: url))
 90        result = scanned
 91        selected = Set(scanned.items.map(\.id))
 92        summary = nil
 93    }
 94
 95    private func runImport() {
 96        guard let result else { return }
 97        importing = true
 98        let chosen = result.items.filter { selected.contains($0.id) }
 99        Task {
100            summary = await EmacsImport.apply(chosen, workspace: workspace)
101            importing = false
102        }
103    }
104}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form