iOS: export names from the open tree, block bands only when needed, org-protocol message !465

merged merged by cmc on 2026-11-22 19:41 UTC · krz/orgstar:issue-230-ios-export-menu into main

11 files changed, +67 −37

Layout: unified · split

Sources/OrgCore/Export/ExportScope.swift +5 −1
@@ -59,7 +59,11 @@ public enum ExportScope {
5959 /// The export's name: the subtree's `EXPORT_FILE_NAME` property when exporting the subtree
6060 /// at `offset`, else the file's last `#+EXPORT_FILE_NAME`; nil when neither is set.
6161 public static func fileName(_ text: String, subtreeAt offset: Int? = nil, settings: OrgSettings = .default) -> String? {
62 let tree = OrgParser.parse(text, defaults: settings)
62 fileName(OrgParser.parse(text, defaults: settings), subtreeAt: offset, settings: settings)
63 }
64
65 /// The export's name from a parsed file; `settings` gives the setup files' keywords.
66 public static func fileName(_ tree: OrgTree, subtreeAt offset: Int? = nil, settings: OrgSettings = .default) -> String? {
6367 if let offset {
6468 let model = DocumentModel(tree: tree)
6569 if let index = model.headings.lastIndex(where: { $0.headingRange.lowerBound <= offset }),
Sources/OrgEditorAppKit/OrgEditor.swift +1 −11
@@ -759,16 +759,6 @@ public final class OrgEditor: NSObject {
759759 invalidateLayout(from: old, to: blocks)
760760 }
761761
762 /// Whether the lines around `range`, and the line after it (a deleted newline joins it to
763 /// them), hold a block delimiter or a fixed-width line.
764 static func mayChangeBlocks(_ text: NSString, _ range: Range<Int>) -> Bool {
765 let start = min(range.lowerBound, text.length)
766 let end = min(range.upperBound + 1, text.length)
767 let lines = text.lineRange(for: NSRange(location: start, length: end - start))
768 let content = text.substring(with: lines)
769 return content.contains("#+") || content.range(of: "(^|\\n)([ \\t]*:|\\*)", options: .regularExpression) != nil
770 }
771
772762 fileprivate func storageEdited(range: NSRange, delta: Int) {
773763 guard !isLoading, let storage = textView.textStorage else { return }
774764 markActive = false
@@ -810,7 +800,7 @@ public final class OrgEditor: NSObject {
810800 if isOrg {
811801 // Walking the tree for blocks costs more than typing; only an edit on a line that
812802 // could start or end one (`#+…`, `: …`) needs it, others move the ranges.
813 if Self.mayChangeBlocks(oldText, edit.range) || Self.mayChangeBlocks(newText, edit.range.lowerBound..<newEnd) {
803 if Presentation.mayChangeBlocks(oldText, edit.range) || Presentation.mayChangeBlocks(newText, edit.range.lowerBound..<newEnd) {
814804 shaded.set(Presentation.shadedBlocks(document.tree))
815805 } else {
816806 shaded.set(oldShaded.map { mapRange($0, through: edit) })
Sources/OrgPresentation/Restyling.swift +10
@@ -151,4 +151,14 @@ extension Presentation {
151151 let oldMapped = oldAffected.lowerBound..<(oldAffected.upperBound >= edit.range.upperBound ? oldAffected.upperBound + delta : newEnd)
152152 return min(oldMapped.lowerBound, newAffected.lowerBound)..<max(oldMapped.upperBound, newAffected.upperBound)
153153 }
154
155 /// Whether the lines around `range`, and the line after it (a deleted newline joins it to
156 /// them), hold a block delimiter or a fixed-width line.
157 public static func mayChangeBlocks(_ text: NSString, _ range: Range<Int>) -> Bool {
158 let start = min(range.lowerBound, text.length)
159 let end = min(range.upperBound + 1, text.length)
160 let lines = text.lineRange(for: NSRange(location: start, length: end - start))
161 let content = text.substring(with: lines)
162 return content.contains("#+") || content.range(of: "(^|\\n)([ \\t]*:|\\*)", options: .regularExpression) != nil
163 }
154164}
Sources/OrgstarMobile/EditorScreen.swift +3 −3
@@ -189,8 +189,8 @@ struct EditorScreen: View {
189189 Button("Encrypt Tagged Entries…", systemImage: "lock.rectangle.stack") { crypt("app.crypt.encrypt-entries") }
190190 Button("Forget Encryption Passphrase", systemImage: "key.slash") { crypt("app.crypt.forget-passphrase") }
191191 }
192 if let text = session.document(of: path)?.text {
193 ExportMenu(path: path, text: text, report: { message = $0 }, exportSubtree: exportSubtree)
192 if let tree = session.document(of: path)?.tree {
193 ExportMenu(path: path, tree: tree, report: { message = $0 }, exportSubtree: exportSubtree)
194194 }
195195 Button("Sync Conflict Copies", systemImage: "doc.on.doc") { sheet = .syncCopies }
196196 Button("Recovered Versions", systemImage: "clock.arrow.circlepath") { sheet = .recovery }
@@ -276,7 +276,7 @@ struct EditorScreen: View {
276276 case .crypt(let request):
277277 CryptScreen(request: request, session: session, editor: editor) { message = $0 }
278278 case .exportSubtree(let caret):
279 if let text = session.document(of: path)?.text { SubtreeExportScreen(path: path, text: text, caret: caret) }
279 if let tree = session.document(of: path)?.tree { SubtreeExportScreen(path: path, tree: tree, caret: caret) }
280280 case .backlinks:
281281 BacklinksScreen(workspace: workspace, session: session, path: path, caret: editor?.editContext().caret ?? 0) { location in
282282 if location.path == path { editor?.reveal(location.start) } else { model.openRequest = .heading(location) }
Sources/OrgstarMobile/ExportMenu.swift +14 −10
@@ -56,17 +56,16 @@ struct OrgExport: Transferable {
5656 let settings: OrgSettings
5757 /// Made on the main actor, where the editor's highlighter is.
5858 let codeStyle: HTMLCodeStyle
59 /// The subtree's `EXPORT_FILE_NAME`, `#+EXPORT_FILE_NAME`'s name, or nil for the file's.
60 let name: String?
5961 /// The subtree at this offset exported alone.
6062 var subtree: Int?
6163 /// Without the title and the page around the text, in the formats that allow it.
6264 var bodyOnly = false
6365
64 /// The subtree's `EXPORT_FILE_NAME`, `#+EXPORT_FILE_NAME`'s name, or the file's, with the
65 /// format's extension.
66 /// `name`, or the file's, with the format's extension.
6667 var fileName: String {
67 ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, subtreeAt: subtree, settings: settings),
68 extension: format.fileExtension)
69 .lastPathComponent
68 ExportScope.output(for: URL(fileURLWithPath: path), name: name, extension: format.fileExtension).lastPathComponent
7069 }
7170
7271 /// Posted with the message, for the screen's message line, when the LaTeX or ASCII export
@@ -165,7 +164,7 @@ struct OrgExport: Transferable {
165164/// Save to Files; or copying it as rich text.
166165struct ExportMenu: View {
167166 let path: String
168 let text: String
167 let tree: OrgTree
169168 @Environment(MobileModel.self) private var model
170169 /// Shows what happened, as the screen's message line does.
171170 var report: (String) -> Void = { _ in }
@@ -176,6 +175,9 @@ struct ExportMenu: View {
176175
177176 var body: some View {
178177 let bodyOnly = kept.isEmpty ? false : ExportSettings.saved(for: path).bodyOnly
178 let text = tree.text
179 // From the open tree: SwiftUI evaluates the menu on every edit.
180 let name = ExportScope.fileName(tree, settings: OrgPreferences.orgSettings())
179181 Menu("Export", systemImage: "square.and.arrow.up") {
180182 ForEach(OrgExport.Format.allCases, id: \.self) { format in
181183 if format.locked {
@@ -184,7 +186,7 @@ struct ExportMenu: View {
184186 Button(format.name) { report(ExportSettings.noBodyOnly) }
185187 } else {
186188 let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(),
187 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme), bodyOnly: bodyOnly)
189 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme), name: name, bodyOnly: bodyOnly)
188190 ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) }
189191 }
190192 }
@@ -274,7 +276,7 @@ struct AgendaCalendars: Transferable {
274276/// The subtree at `caret` shared alone, in each of the export formats.
275277struct SubtreeExportScreen: View {
276278 let path: String
277 let text: String
279 let tree: OrgTree
278280 let caret: Int
279281 @Environment(MobileModel.self) private var model
280282 @Environment(\.dismiss) private var dismiss
@@ -282,12 +284,14 @@ struct SubtreeExportScreen: View {
282284 var body: some View {
283285 NavigationStack {
284286 let bodyOnly = ExportSettings.saved(for: path).bodyOnly
287 let name = ExportScope.fileName(tree, subtreeAt: caret, settings: OrgPreferences.orgSettings())
285288 List(OrgExport.Format.allCases.filter { !$0.locked }, id: \.self) { format in
286289 if bodyOnly, !format.allowsBodyOnly {
287290 LabeledContent(format.name) { Text("No body-only export") }.foregroundStyle(.secondary)
288291 } else {
289 let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(),
290 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme), subtree: caret, bodyOnly: bodyOnly)
292 let export = OrgExport(text: tree.text, path: path, format: format, settings: OrgPreferences.orgSettings(),
293 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme), name: name,
294 subtree: caret, bodyOnly: bodyOnly)
291295 ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) }
292296 }
293297 }
Sources/OrgstarMobile/MobileEditor+Styling.swift +4 −1
@@ -192,7 +192,10 @@ extension MobileEditor {
192192 DispatchQueue.main.async { [weak self] in
193193 guard let self, let range = self.restyleScheduled else { return }
194194 self.restyleScheduled = nil
195 self.shaded.set(Presentation.shadedBlocks(self.document.tree), folded: [])
195 if self.blocksMayChange {
196 self.blocksMayChange = false
197 self.shaded.set(Presentation.shadedBlocks(self.document.tree), folded: [])
198 }
196199 self.restyle(range)
197200 self.updateHidden()
198201 self.textView.typingAttributes = self.theme.base
Sources/OrgstarMobile/MobileEditor.swift +10
@@ -144,6 +144,8 @@ final class MobileEditor: NSObject, DocumentBuffer {
144144 static let imageCache = NSCache<NSString, UIImage>()
145145 var loading = false
146146 var restyleScheduled: Range<Int>?
147 /// An edit since the last restyle touched a line that can start or end a block.
148 var blocksMayChange = false
147149 /// Text before its end is styled; after it, the text waits for the background passes.
148150 var styled = StyledExtent()
149151 var stylingScheduled = false
@@ -583,6 +585,14 @@ final class MobileEditor: NSObject, DocumentBuffer {
583585 styled.map(through: edit, in: newText)
584586 let affected = Presentation.restyleRange(for: edit, old: (oldTree, oldText), new: (document.tree, newText))
585587 vim.changed(edit)
588 // Walking the tree for blocks costs more than typing; only an edit on a line that could
589 // start or end one needs it, others move the ranges.
590 let newEnd = edit.range.lowerBound + (replacement as NSString).length
591 if Presentation.mayChangeBlocks(oldText, edit.range) || Presentation.mayChangeBlocks(newText, edit.range.lowerBound..<newEnd) {
592 blocksMayChange = true
593 } else {
594 shaded.set(ViewState(selection: shaded.all).mapped(through: [edit]).selection, folded: [])
595 }
586596 for table in shrunk.map(through: edit, in: textView.textStorage.string, command: commandEditing) { scheduleRestyle(table) }
587597 fold = FoldState(
588598 folds: Self.map(fold.folds, old, delta), bodyFolds: Self.map(fold.bodyFolds, old, delta),
Sources/OrgstarMobile/OrgstarMobileApp.swift +15 −10
@@ -350,16 +350,7 @@ struct RootView: View {
350350 model.reloadConfig()
351351 Task { await workspace.refresh() }
352352 }
353 .onOpenURL { url in
354 if let link = AppLink(url: url) {
355 open(link)
356 } else if let request = CaptureRequest(link: url.absoluteString) {
357 capture = request
358 } else if case .storeLink(let link, let title)? = OrgProtocol.parse(url.absoluteString) {
359 message = StoredLink.add(StoredLink(link: link, description: title), to: &model.storedLinks)
360 UIPasteboard.general.string = link
361 }
362 }
353 .onOpenURL(perform: handle)
363354 // The Live Activity follows the clock, wherever it starts or stops.
364355 .task {
365356 LiveClock.publish(model.clock)
@@ -401,6 +392,20 @@ struct RootView: View {
401392 showWaiting()
402393 }
403394
395 /// A widget's or the Live Activity's link, or an org-protocol URL.
396 private func handle(_ url: URL) {
397 if let link = AppLink(url: url) {
398 open(link)
399 } else if let request = CaptureRequest(link: url.absoluteString) {
400 capture = request
401 } else if case .storeLink(let link, let title)? = OrgProtocol.parse(url.absoluteString) {
402 message = StoredLink.add(StoredLink(link: link, description: title), to: &model.storedLinks)
403 UIPasteboard.general.string = link
404 } else if url.scheme == "org-protocol" {
405 message = "orgstar handles org-protocol capture and store-link"
406 }
407 }
408
404409 /// A widget's or the Live Activity's link.
405410 private func open(_ link: AppLink) {
406411 switch link {
Sources/OrgstarMobile/ReaderScreen.swift +1 −1
@@ -110,7 +110,7 @@ struct ReaderScreen: View {
110110 ToolbarItemGroup(placement: .primaryAction) {
111111 Button("Outline", systemImage: "list.bullet.indent") { showingOutline = true }.disabled(outline.isEmpty)
112112 if let path = shownPath {
113 if let text = document?.text { ExportMenu(path: path, text: text) { message = $0 } }
113 if let tree = document?.tree { ExportMenu(path: path, tree: tree) { message = $0 } }
114114 NavigationLink(value: EditorRoute(path: path, offset: nil)) { Label("Edit", systemImage: "square.and.pencil") }
115115 }
116116 }
Tests/OrgCoreTests/ExportScopeTests.swift +2
@@ -62,6 +62,8 @@ struct ExportScopeTests {
6262 #expect(ExportScope.fileName(Self.file, subtreeAt: Self.project) == "project-notes")
6363 #expect(ExportScope.fileName(Self.file, subtreeAt: 0) == nil)
6464 #expect(ExportScope.fileName("#+EXPORT_FILE_NAME: out/x\n* A\n") == "out/x")
65 #expect(ExportScope.fileName(OrgParser.parse(Self.file), subtreeAt: Self.project) == "project-notes")
66 #expect(ExportScope.fileName(OrgParser.parse("#+EXPORT_FILE_NAME: out/x\n* A\n")) == "out/x")
6567 #expect(ExportScope.output(for: source, name: "out/x", extension: "html").path == "/notes/out/x.html")
6668 #expect(ExportScope.output(for: source, name: "out/x.txt", extension: "html").path == "/notes/out/x.html")
6769 #expect(ExportScope.output(for: source, name: "/tmp/abs", extension: "md").path == "/tmp/abs.md")
docs/manual/guide/15-alongside-emacs.org +2
@@ -31,6 +31,8 @@ Emacs and Syncthing don't use file coordination, so these checks can't lock them
3131
3232If the file keeps changing through all three attempts, the save fails with an error and your edits stay unsaved.
3333
34In the Mac App Store version, when the folder doesn't let orgstar create the temporary file, orgstar writes the file in place instead.
35
3436** Automatic and explicit saving
3537
3638With =save = "automatic"= (the default), orgstar saves a file one second after you stop typing. With ="explicit"=, only =⌘S= (Save) and =⌥⌘S= (Save All) save. See [[file:13-configuration.org][Configuration]]. The iOS app always saves automatically.