Range-diff !450

back to !450 Export: per-file settings, and subtree and body-only for the export commands

1:  7294192 ! 1:  69626ad Export: per-file settings, and subtree and body-only for the export commands
    @@ Sources/OrgApp/ExportSettings.swift (new)
     +        return (subtree, bodyOnly)
     +    }
     +
    ++    /// The message after an export to the file named `name`, saying what was exported when it
    ++    /// was not the whole file as a whole document.
    ++    public static func exported(_ name: String, subtree: Bool, bodyOnly: Bool) -> String {
    ++        switch (subtree, bodyOnly) {
    ++        case (true, true): "Exported the subtree's body to \(name)"
    ++        case (true, false): "Exported the subtree to \(name)"
    ++        case (false, true): "Exported the body to \(name)"
    ++        case (false, false): "Exported to \(name)"
    ++        }
    ++    }
    ++
     +    private static func all(_ defaults: UserDefaults) -> [String: ExportSettings] {
     +        defaults.data(forKey: key).flatMap { try? JSONDecoder().decode([String: ExportSettings].self, from: $0) } ?? [:]
     +    }
    @@ Sources/OrgEditorAppKit/EditorCommands.swift: public enum EditorCommands {
              EditorCommand(id: "app.recovery", title: "Recovery Versions…", action: .app),
     
      ## Sources/Orgstar/ContentView.swift ##
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +             do {
    +                 let target = try ICalendarFiles.export(whole, path: path, settings: defaults, options: OrgPreferences.icalendarOptions(),
    +                                                        subtree: subtree ? caret : nil, destination: destination)
    +-                commands.show("Exported to \(target.lastPathComponent)")
    ++                commands.show(ExportSettings.exported(target.lastPathComponent, subtree: subtree, bodyOnly: bodyOnly))
    +                 if open { NSWorkspace.shared.open(target) }
    +             } catch {
    +                 commands.show("Can't export: \(error.localizedDescription)")
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +             do {
    +                 try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
    +                 try content.write(to: target, atomically: true, encoding: .utf8)
    +-                commands.show("Exported to \(target.lastPathComponent)")
    ++                commands.show(ExportSettings.exported(target.lastPathComponent, subtree: subtree, bodyOnly: bodyOnly))
    +                 if open { NSWorkspace.shared.open(target) }
    +             } catch {
    +                 commands.show("Can't write \(target.lastPathComponent): \(error.localizedDescription)")
    +             }
    +-        case "latex" where exportNative("LaTeX", extension: "tex", whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, settings: defaults, export: {
    ++        case "latex" where exportNative("LaTeX", extension: "tex", whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, bodyOnly: bodyOnly, settings: defaults, export: {
    +             try LaTeXExport.document(text, settings: defaults, path: path, bodyOnly: bodyOnly, author: NSFullUserName(), language: OrgPreferences.exportLanguage(), file: true)
    +         }):
    +             break
    +-        case "text" where exportNative("plain text", extension: "txt", whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, settings: defaults, export: {
    ++        case "text" where exportNative("plain text", extension: "txt", whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, bodyOnly: bodyOnly, settings: defaults, export: {
    +             try ASCIIExport.document(text, settings: defaults, path: path, bodyOnly: bodyOnly, author: NSFullUserName(), language: OrgPreferences.exportLanguage(), file: true)
    +         }):
    +             break
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +                 let data = try ODTExport.document(text, settings: defaults, path: path, author: NSFullUserName(), language: OrgPreferences.exportLanguage())
    +                 try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
    +                 try data.write(to: target, options: .atomic)
    +-                commands.show("Exported to \(target.lastPathComponent)")
    ++                commands.show(ExportSettings.exported(target.lastPathComponent, subtree: subtree, bodyOnly: bodyOnly))
    +                 if open { NSWorkspace.shared.open(target) }
    +             } catch {
    +                 commands.show("Can't export \(target.lastPathComponent): \(error.localizedDescription)")
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +                     }
    +                     try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
    +                     try data.write(to: target, options: .atomic)
    +-                    commands.show("Exported to \(target.lastPathComponent)")
    ++                    commands.show(ExportSettings.exported(target.lastPathComponent, subtree: subtree, bodyOnly: bodyOnly))
    +                     if open { NSWorkspace.shared.open(target) }
    +                 } catch {
    +                     commands.show("Can't export \(target.lastPathComponent): \(error)")
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +                         try FileManager.default.moveItem(at: written, to: destination)
    +                         written = destination
    +                     }
    +-                    commands.show("Exported to \(written.lastPathComponent)")
    ++                    commands.show(ExportSettings.exported(written.lastPathComponent, subtree: subtree, bodyOnly: bodyOnly))
    +                     if open { NSWorkspace.shared.open(written) }
    +                 } catch is CancellationError {
    +                     commands.show("Export canceled")
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +     /// Writes what `export` makes, as ox does for `format`. False when the native export can't
    +     /// match Emacs and Emacs is installed to export instead; otherwise the reason is shown.
    +     private func exportNative(
    +-        _ format: String, extension ext: String, whole: String, url: URL, to destination: URL?, open: Bool, subtree: Int?, settings: OrgSettings,
    ++        _ format: String, extension ext: String, whole: String, url: URL, to destination: URL?, open: Bool, subtree: Int?, bodyOnly: Bool, settings: OrgSettings,
    +         export: () throws -> String
    +     ) -> Bool {
    +         let content: String
    +@@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
    +         do {
    +             try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
    +             try content.write(to: target, atomically: true, encoding: .utf8)
    +-            commands.show("Exported to \(target.lastPathComponent)")
    ++            commands.show(ExportSettings.exported(target.lastPathComponent, subtree: subtree != nil, bodyOnly: bodyOnly))
    +             if open { NSWorkspace.shared.open(target) }
    +         } catch {
    +             commands.show("Can't write \(target.lastPathComponent): \(error.localizedDescription)")
     @@ Sources/Orgstar/ContentView.swift: struct ContentView: View {
                  return commands.show(Self.unavailable(EditorCommands.all.first { $0.id == id }?.title ?? id))
              }
    @@ Tests/OrgAppTests/ExportSettingsTests.swift (new)
     +        }
     +    }
     +
    ++    @Test func theMessageSaysWhatWasExported() {
    ++        #expect(ExportSettings.exported("a.html", subtree: false, bodyOnly: false) == "Exported to a.html")
    ++        #expect(ExportSettings.exported("a.html", subtree: true, bodyOnly: false) == "Exported the subtree to a.html")
    ++        #expect(ExportSettings.exported("a.html", subtree: false, bodyOnly: true) == "Exported the body to a.html")
    ++        #expect(ExportSettings.exported("a.html", subtree: true, bodyOnly: true) == "Exported the subtree's body to a.html")
    ++    }
    ++
     +    /// Body only is refused for the formats the dialog disables it for; the agenda files'
     +    /// calendars follow neither choice.
     +    @Test func bodyOnlyIsRefusedForWholeDocumentFormats() throws {
    @@ docs/manual/guide/03-keys.org: When a key appears in several rows, it runs the c
      | Export to Markdown (=app.export.markdown=) | =C-c C-e m m= | — | =SPC m e m m= (N) | Exports to Markdown (=org-md-export-to-markdown=). |
     
      ## docs/manual/guide/12-export.org ##
    -@@ docs/manual/guide/12-export.org: The keys follow Emacs's export dispatcher (=org-export-dispatch=). =C-c C-e= on
    +@@ docs/manual/guide/12-export.org: Export works on the file in the current editor, using its text as it is now, sav
      
    - All fourteen commands are in File ▸ Export and in the command palette. They write the export beside the Org file, with the same name and the format's extension: =.html=, =.md=, =.pdf=, =.docx=, =.rtf=, =.tex=, =.odt=, =.txt= or =.ics=. The two agenda-file commands are the exceptions; see [[*iCalendar export][iCalendar export]]. =#+EXPORT_FILE_NAME:= names it instead, as in Org: relative to the Org file's folder (or absolute, or starting with =~=), with its extension replaced by the format's, so =#+EXPORT_FILE_NAME: out/report= writes =out/report.html=. A file already there is replaced. The message area shows "Exported to /name/".
    + The keys follow Emacs's export dispatcher (=org-export-dispatch=). =C-c C-e= on its own is only a prefix: pause after it and the key hints list the formats. The =SPC m e= keys work in normal state; the =C-c C-e= keys work in every Doom state. The Mac preset has no keys for the single formats; its =⇧⌘E= opens the export dialog.
      
    +-All fourteen commands are in File ▸ Export and in the command palette. They write the export beside the Org file, with the same name and the format's extension: =.html=, =.md=, =.pdf=, =.docx=, =.rtf=, =.tex=, =.odt=, =.txt= or =.ics=. The two agenda-file commands are the exceptions; see [[*iCalendar export][iCalendar export]]. =#+EXPORT_FILE_NAME:= names it instead, as in Org: relative to the Org file's folder (or absolute, or starting with =~=), with its extension replaced by the format's, so =#+EXPORT_FILE_NAME: out/report= writes =out/report.html=. A file already there is replaced. The message area shows "Exported to /name/".
    ++All fourteen commands are in File ▸ Export and in the command palette. They write the export beside the Org file, with the same name and the format's extension: =.html=, =.md=, =.pdf=, =.docx=, =.rtf=, =.tex=, =.odt=, =.txt= or =.ics=. The two agenda-file commands are the exceptions; see [[*iCalendar export][iCalendar export]]. =#+EXPORT_FILE_NAME:= names it instead, as in Org: relative to the Org file's folder (or absolute, or starting with =~=), with its extension replaced by the format's, so =#+EXPORT_FILE_NAME: out/report= writes =out/report.html=. A file already there is replaced. The message area shows "Exported to /name/", or "Exported the subtree to /name/", "Exported the body to /name/" or "Exported the subtree's body to /name/" when the export was not the whole file as a whole document.
    ++
     +The twelve commands that export the current file export the whole file as a whole document, unless the file is set otherwise. Two commands set it, as =C-s= and =C-b= do in Org's dispatcher:
     +
     +| Command                         | Emacs preset and Doom | Effect on the file's export commands                              |
    @@ docs/manual/guide/12-export.org: The keys follow Emacs's export dispatcher (=org
     +| Turn Body-Only Export On or Off | =C-c C-e C-b=         | They leave out the title and the page; see /Body only/ below.     |
     +
     +Both are in the command palette, and the message area says what is now on. The choice is kept for the file, and it is the same one the export dialog shows as /Scope/ and /Body only/, so setting it in the dialog sets it for the commands too. With subtree export on and the caret before the first heading, a command says "Not in a subtree" and writes nothing. With body-only export on, a command for PDF, Word, RTF, plain text, ODT or iCalendar says "This format has no body-only export. Turn body-only export off for this file first." and writes nothing. The two agenda-file commands follow neither.
    -+
    + 
      The [[file:../install.org][Mac App Store version]] has the thirteen commands that don't need Emacs; File ▸ Export, the export dialog and the command palette leave out Export to PDF with Emacs, and a key bound to it runs Export to PDF. When the export file, or an =#+INCLUDE:= or =#+SETUPFILE:=, is outside your folders, orgstar first asks for access to its folder; for the export file, Save Elsewhere… instead picks another place. See [[file:../install.org][Install]].
      
    - ** The export dialog
     @@ docs/manual/guide/12-export.org: The [[file:../install.org][Mac App Store version]] has the thirteen commands tha
      - *Margins*, *Font* and *Page numbers*, for PDF: see /PDF export/.
      - *Open after export*: opens the file in its default app when the export finishes.