Commit 30fb15c9bc
Verified · cmc
Changed files (12)
- Sources/OrgApp/CompletionSources.swift added +45
- Sources/OrgCore/Commands/PropertyCommands.swift +1 −1
- Sources/OrgCore/Completion/IndexCompletion.swift added +134
- Sources/OrgIndex/CompletionQueries.swift added +65
- Sources/Orgstar/ContentView.swift +1
- Sources/Orgstar/PaneView.swift +6
- Sources/OrgstarMobile/EditorScreen.swift +3
- Tests/OrgAppTests/CompletionSourcesTests.swift added +55
- Tests/OrgIndexTests/CompletionQueriesTests.swift added +49
- docs/manual/guide/02-the-editor.org +35 −3
- docs/manual/guide/14-ios.org +4
- docs/manual/guide/15-alongside-emacs.org +11
Sources/OrgApp/CompletionSources.swift added +45 · view
| @@ -0,0 +1,45 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | import OrgIndex | |
| 4 | ||
| 5 | /// What completion takes from the index and the other files, for both apps' editors. | |
| 6 | public enum CompletionSources { | |
| 7 | /// Answers `query` from `index`, leaving out `path`, whose buffer gives its own. Reads the | |
| 8 | /// index, so it runs off the main actor; a failed read answers nothing. | |
| 9 | @concurrent | |
| 10 | public nonisolated static func answer(_ query: CompletionQuery, index: IndexStore, excluding path: String?) async -> CompletionResult { | |
| 11 | switch query { | |
| 12 | case .linkTargets: | |
| 13 | return CompletionResult(items: (try? index.linkTargets(excluding: path)) ?? []) | |
| 14 | case .denoteNotes: | |
| 15 | let notes = ((try? index.denoteNodes()) ?? []).filter { $0.location.path != path } | |
| 16 | return CompletionResult(items: notes.map { CompletionItem(title: $0.title, id: $0.id, path: $0.location.path, start: 0, heading: false) }) | |
| 17 | case .propertyValues(let name): | |
| 18 | guard let values = try? index.propertyValues(name, excluding: path) else { return CompletionResult() } | |
| 19 | return CompletionResult(strings: values.allowed + values.used) | |
| 20 | case .words(let prefix): | |
| 21 | return CompletionResult(strings: (try? index.words(prefix, excluding: path)) ?? []) | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | /// `org-id-get-create` on a heading in another file, through its buffer when it's open: | |
| 26 | /// the new ID, or nil when the heading isn't where the index put it. | |
| 27 | @MainActor | |
| 28 | public static func createID(_ item: CompletionItem, session: DocumentSession, newID: String = UUID().uuidString.lowercased()) -> String? { | |
| 29 | guard item.heading, let text = session.text(of: item.path) else { return nil } | |
| 30 | let ns = text as NSString | |
| 31 | guard item.start < ns.length else { return nil } | |
| 32 | var end = 0 | |
| 33 | ns.getLineStart(nil, end: nil, contentsEnd: &end, for: NSRange(location: item.start, length: 0)) | |
| 34 | let line = ns.substring(with: NSRange(location: item.start, length: end - item.start)) | |
| 35 | guard line.hasPrefix("*"), line.contains(item.title) else { return nil } | |
| 36 | guard case .commit = session.perform(CreateID(newID: { newID }), path: item.path, at: item.start, expect: (item.start, line)) else { return nil } | |
| 37 | return newID | |
| 38 | } | |
| 39 | ||
| 40 | /// The text of the open buffers other than `path`, for word completion. | |
| 41 | @MainActor | |
| 42 | public static func otherBuffers(_ session: DocumentSession, excluding path: String?) -> [String] { | |
| 43 | session.openPaths.filter { $0 != path }.compactMap { session.text(of: $0) } | |
| 44 | } | |
| 45 | } | |
Sources/OrgCore/Commands/PropertyCommands.swift +1 −1 · view
| @@ -95,7 +95,7 @@ public enum PropertyValues { | ||
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /// `(read-from-string (concat "(" vals ")"))` as strings: quoted strings, numbers, symbols. |
| 98 | static func readList(_ s: String) -> [String] { | |
| 98 | public static func readList(_ s: String) -> [String] { | |
| 99 | 99 | var values: [String] = [] |
| 100 | 100 | for m in s.matches(of: /"((?:[^"\\]|\\.)*)"|(\S+)/) { |
| 101 | 101 | if let quoted = m.1 { |
Sources/OrgCore/Completion/IndexCompletion.swift added +134 · view
| @@ -0,0 +1,134 @@ | ||
| 1 | import Foundation | |
| 2 | ||
| 3 | /// A heading or file a link completes to: from the buffer, or from the index for other files. | |
| 4 | public struct CompletionItem: Sendable, Hashable { | |
| 5 | public var title: String | |
| 6 | /// Its `ID`, or a Denote note's identifier; nil when the heading has none yet. | |
| 7 | public var id: String? | |
| 8 | public var path: String | |
| 9 | /// Where its heading starts; 0 for a file. | |
| 10 | public var start: Int | |
| 11 | /// A heading, not a file. | |
| 12 | public var heading: Bool | |
| 13 | ||
| 14 | public init(title: String, id: String?, path: String, start: Int, heading: Bool) { | |
| 15 | self.title = title | |
| 16 | self.id = id | |
| 17 | self.path = path | |
| 18 | self.start = start | |
| 19 | self.heading = heading | |
| 20 | } | |
| 21 | } | |
| 22 | ||
| 23 | /// What a completion asks the index, off the main thread, after the buffer's candidates show. | |
| 24 | public enum CompletionQuery: Sendable, Hashable { | |
| 25 | /// Headings and files in every folder, for `[[id:`. | |
| 26 | case linkTargets | |
| 27 | /// Denote's notes, for `[[denote:`. | |
| 28 | case denoteNotes | |
| 29 | /// Values used for a property, and its `_ALL` values. | |
| 30 | case propertyValues(String) | |
| 31 | /// Words starting with the abbreviation. | |
| 32 | case words(String) | |
| 33 | } | |
| 34 | ||
| 35 | /// What the index answers. | |
| 36 | public struct CompletionResult: Sendable, Equatable { | |
| 37 | public var items: [CompletionItem] = [] | |
| 38 | public var strings: [String] = [] | |
| 39 | ||
| 40 | public init(items: [CompletionItem] = [], strings: [String] = []) { | |
| 41 | self.items = items | |
| 42 | self.strings = strings | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | extension OrgCompletion { | |
| 47 | /// `completion` with what the index answered for its query added to the buffer's | |
| 48 | /// candidates, matching what's typed. | |
| 49 | public static func filled(_ completion: Completion, with result: CompletionResult, fuzzy: Bool) -> Completion { | |
| 50 | var filled = completion | |
| 51 | switch completion.kind { | |
| 52 | case .idLink, .denoteLink: | |
| 53 | let items = completion.targets + result.items | |
| 54 | filled.setItems(items, fuzzy: fuzzy) | |
| 55 | case .propertyValue: | |
| 56 | filled.pool = completion.pool + result.strings | |
| 57 | filled.candidates = matching(filled.pool, completion.typed, fuzzy: fuzzy, ignoringCase: true, sorted: false) | |
| 58 | case .word: | |
| 59 | filled = Dabbrev.adding(result.strings, to: completion) | |
| 60 | default: | |
| 61 | break | |
| 62 | } | |
| 63 | filled.query = nil | |
| 64 | return filled | |
| 65 | } | |
| 66 | ||
| 67 | /// `[[id:` and `[[denote:`: the link type, what's typed after it, and the whole link | |
| 68 | /// typed so far, through `]]` after the caret. | |
| 69 | static func linkTarget(_ context: EditContext, lineStart: Int, toHere: String) -> (type: String, typed: Range<Int>, link: Range<Int>)? { | |
| 70 | guard let match = toHere.firstMatch(of: /\[\[(id|denote):([^\[\]]*)$/) else { return nil } | |
| 71 | let ns = context.text as NSString | |
| 72 | let caret = lineStart + (toHere as NSString).length | |
| 73 | let linkStart = lineStart + toHere.utf16.distance(from: toHere.utf16.startIndex, to: match.range.lowerBound) | |
| 74 | let typedStart = caret - String(match.2).utf16.count | |
| 75 | var end = caret | |
| 76 | if end + 2 <= ns.length, ns.substring(with: NSRange(location: end, length: 2)) == "]]" { end += 2 } | |
| 77 | return (String(match.1), typedStart..<caret, linkStart..<end) | |
| 78 | } | |
| 79 | ||
| 80 | /// The open file's headings and its own ID, for `[[id:`. | |
| 81 | static func bufferTargets(_ context: EditContext) -> [CompletionItem] { | |
| 82 | let path = context.options.filePath ?? "" | |
| 83 | let model = DocumentModel(tree: context.tree) | |
| 84 | let file = model.property("ID", of: nil).map { id in | |
| 85 | let title = context.text.firstMatch(of: /(?im)^[ \t]*#\+title:[ \t]*(.*?)[ \t]*$/).map { String($0.1) } | |
| 86 | return CompletionItem(title: title ?? ((path as NSString).lastPathComponent as NSString).deletingPathExtension, id: id.value, | |
| 87 | path: path, start: 0, heading: false) | |
| 88 | } | |
| 89 | return (file.map { [$0] } ?? []) + model.headings.map { CompletionItem(title: $0.title, id: $0.id, path: path, start: $0.headingRange.lowerBound, heading: true) } | |
| 90 | } | |
| 91 | ||
| 92 | /// Property values in the buffer: those `org-property-get-allowed-values` gives at the | |
| 93 | /// caret, then those headings give `name`. | |
| 94 | static func bufferValues(_ name: String, _ context: EditContext) -> [String] { | |
| 95 | let model = DocumentModel(tree: context.tree) | |
| 96 | let allowed = PropertyValues.allowed(name, at: context.caret, text: context.text, tree: context.tree)?.values.filter { !$0.isEmpty } ?? [] | |
| 97 | let key = name.uppercased() | |
| 98 | return allowed + (model.fileProperties + model.headings.flatMap(\.properties)).filter { $0.key.uppercased() == key && !$0.value.isEmpty }.map(\.value) | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | extension Completion { | |
| 103 | /// Candidates for link items: titles, with the file's name where titles repeat. | |
| 104 | mutating func setItems(_ all: [CompletionItem], fuzzy: Bool) { | |
| 105 | var counts: [String: Int] = [:] | |
| 106 | for item in all { counts[item.title, default: 0] += 1 } | |
| 107 | var labels: [String: CompletionItem] = [:] | |
| 108 | var order: [String] = [] | |
| 109 | for item in all { | |
| 110 | var label = counts[item.title, default: 0] > 1 ? "\(item.title) (\((item.path as NSString).lastPathComponent))" : item.title | |
| 111 | var n = 2 | |
| 112 | while labels[label] != nil { | |
| 113 | label = "\(item.title) (\((item.path as NSString).lastPathComponent) \(n))" | |
| 114 | n += 1 | |
| 115 | } | |
| 116 | labels[label] = item | |
| 117 | order.append(label) | |
| 118 | } | |
| 119 | pool = order | |
| 120 | candidates = OrgCompletion.matching(order, typed, fuzzy: fuzzy, ignoringCase: true) | |
| 121 | items = candidates.compactMap { labels[$0] } | |
| 122 | targets = all | |
| 123 | } | |
| 124 | ||
| 125 | /// The item behind a candidate of a link completion. | |
| 126 | public func item(_ candidate: String) -> CompletionItem? { | |
| 127 | candidates.firstIndex(of: candidate).flatMap { items.indices.contains($0) ? items[$0] : nil } | |
| 128 | } | |
| 129 | ||
| 130 | /// The link a link completion inserts for `item`, given its ID. | |
| 131 | public func link(_ item: CompletionItem, id: String) -> String { | |
| 132 | Links.makeString((kind == .denoteLink ? "denote:" : "id:") + id, item.title) | |
| 133 | } | |
| 134 | } | |
Sources/OrgIndex/CompletionQueries.swift added +65 · view
| @@ -0,0 +1,65 @@ | ||
| 1 | import Foundation | |
| 2 | import GRDB | |
| 3 | import OrgCore | |
| 4 | ||
| 5 | /// What completion asks the index: link targets, property values and words. The open file's | |
| 6 | /// own come from its buffer, so `excluding` leaves its rows out. | |
| 7 | extension IndexStore { | |
| 8 | /// Every heading in org files, and every file with an ID, with its ID if it has one: | |
| 9 | /// what `[[id:` completes. | |
| 10 | public func linkTargets(excluding path: String? = nil) throws -> [CompletionItem] { | |
| 11 | try database.read { db in | |
| 12 | let files = try Row.fetchAll(db, sql: """ | |
| 13 | SELECT path, org_id, title FROM files WHERE org_id IS NOT NULL AND kind = 'org' AND path IS NOT ? ORDER BY path | |
| 14 | """, arguments: [path]).map { row in | |
| 15 | let path: String = row["path"] | |
| 16 | let title = (row["title"] as String?) ?? ((path as NSString).lastPathComponent as NSString).deletingPathExtension | |
| 17 | return CompletionItem(title: title, id: row["org_id"], path: path, start: 0, heading: false) | |
| 18 | } | |
| 19 | let headings = try Row.fetchAll(db, sql: """ | |
| 20 | SELECT f.path, h.title, h.org_id, h.start_offset FROM headings h JOIN files f ON f.id = h.file_id | |
| 21 | WHERE f.kind = 'org' AND f.path IS NOT ? ORDER BY f.path, h.ordinal | |
| 22 | """, arguments: [path]).map { row in | |
| 23 | CompletionItem(title: row["title"], id: row["org_id"], path: row["path"], start: row["start_offset"], heading: true) | |
| 24 | } | |
| 25 | return files + headings | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | /// Values headings give `name` themselves, most used first, and the values its `_ALL` | |
| 30 | /// properties allow, in order. | |
| 31 | public func propertyValues(_ name: String, excluding path: String? = nil) throws -> (used: [String], allowed: [String]) { | |
| 32 | try database.read { db in | |
| 33 | let used = try String.fetchAll(db, sql: """ | |
| 34 | SELECT p.value FROM properties p JOIN headings h ON h.id = p.heading_id JOIN files f ON f.id = h.file_id | |
| 35 | WHERE upper(p.key) = upper(?) AND p.inherited = 0 AND f.path IS NOT ? AND p.value != '' | |
| 36 | GROUP BY p.value ORDER BY count(*) DESC, p.value | |
| 37 | """, arguments: [name, path]) | |
| 38 | let allowed = try String.fetchAll(db, sql: """ | |
| 39 | SELECT p.value FROM properties p JOIN headings h ON h.id = p.heading_id JOIN files f ON f.id = h.file_id | |
| 40 | WHERE upper(p.key) = upper(?) AND p.inherited = 0 AND f.path IS NOT ? | |
| 41 | """, arguments: [name + "_ALL", path]) | |
| 42 | return (used, allowed.flatMap(PropertyValues.readList).filter { $0 != ":ETC" }) | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | /// Words starting with `prefix`, ignoring case, in headings and text before first | |
| 47 | /// headings that the full-text index finds for it. | |
| 48 | public func words(_ prefix: String, excluding path: String? = nil, limit: Int = 50) throws -> [String] { | |
| 49 | let term = prefix.lowercased().split { !$0.isLetter && !$0.isNumber }.first.map(String.init) ?? "" | |
| 50 | guard !term.isEmpty else { return [] } | |
| 51 | let match = "\"" + term + "\"*" | |
| 52 | let texts = try database.read { db in | |
| 53 | try String.fetchAll(db, sql: """ | |
| 54 | SELECT headings_fts.title || char(10) || headings_fts.body FROM headings_fts | |
| 55 | JOIN headings h ON h.id = headings_fts.rowid JOIN files f ON f.id = h.file_id | |
| 56 | WHERE headings_fts MATCH ? AND f.path IS NOT ? LIMIT 200 | |
| 57 | """, arguments: [match, path]) | |
| 58 | + String.fetchAll(db, sql: """ | |
| 59 | SELECT files_fts.body FROM files_fts JOIN files f ON f.id = files_fts.rowid | |
| 60 | WHERE files_fts MATCH ? AND f.path IS NOT ? LIMIT 50 | |
| 61 | """, arguments: [match, path]) | |
| 62 | } | |
| 63 | return Dabbrev.words(prefix, in: texts, limit: limit) | |
| 64 | } | |
| 65 | } | |
Sources/Orgstar/ContentView.swift +1 · view
| @@ -361,6 +361,7 @@ struct ContentView: View { | ||
| 361 | 361 | } |
| 362 | 362 | clock.remindAtEffort(session) |
| 363 | 363 | commands.knownTags = { [index = workspace.index] in (try? index.tags()) ?? [] } |
| 364 | commands.completionIndex = workspace.index | |
| 364 | 365 | commands.mainWindowOpen = true |
| 365 | 366 | commands.whatsNew = WhatsNew.pending(firstLaunch: noFolders && !welcomeShown) |
| 366 | 367 | if noFolders, !welcomeShown { |
Sources/Orgstar/PaneView.swift +6 · view
| @@ -132,6 +132,12 @@ struct PaneView: NSViewRepresentable { | ||
| 132 | 132 | editor.onImage = { [weak commands] in commands?.imageHandler?($0, $1) } |
| 133 | 133 | editor.knownTags = { [weak commands] in MainActor.assumeIsolated { commands?.knownTags() ?? [] } } |
| 134 | 134 | editor.onPrompt = { [weak commands] prompt, reply in commands?.ask(prompt, reply: reply) } |
| 135 | editor.completionSource = { [weak commands] query in | |
| 136 | guard let index = await commands?.completionIndex else { return CompletionResult() } | |
| 137 | return await CompletionSources.answer(query, index: index, excluding: path) | |
| 138 | } | |
| 139 | editor.createID = { [weak session] item in session.flatMap { CompletionSources.createID(item, session: $0) } } | |
| 140 | editor.otherBuffers = { [weak session] in session.map { CompletionSources.otherBuffers($0, excluding: path) } ?? [] } | |
| 135 | 141 | editor.onEffect = { [weak commands, weak session] effect in |
| 136 | 142 | switch effect { |
| 137 | 143 | case .storedLinkUsed(let link): commands?.storedLinks.removeAll { $0.link == link } |
Sources/OrgstarMobile/EditorScreen.swift +3 · view
| @@ -419,6 +419,9 @@ struct EditorScreen: View { | ||
| 419 | 419 | /// The editor's callbacks go to this screen: it's shown here, or its view here took focus. |
| 420 | 420 | private func bind(_ editor: MobileEditor) { |
| 421 | 421 | editor.knownTags = { [index = workspace.index] in (try? index.tags()) ?? [] } |
| 422 | editor.completionSource = { [index = workspace.index, path] query in await CompletionSources.answer(query, index: index, excluding: path) } | |
| 423 | editor.createID = { [weak session] item in session.flatMap { CompletionSources.createID(item, session: $0) } } | |
| 424 | editor.otherBuffers = { [weak session, path] in session.map { CompletionSources.otherBuffers($0, excluding: path) } ?? [] } | |
| 422 | 425 | editor.onChange = { [weak session] in session?.bufferChanged(path) } |
| 423 | 426 | editor.onMessage = { message = $0 } |
| 424 | 427 | editor.onPrompt = { prompt, reply in self.prompt = PromptRequest(prompt: prompt, reply: reply) } |
Tests/OrgAppTests/CompletionSourcesTests.swift added +55 · view
| @@ -0,0 +1,55 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | import OrgDocument | |
| 4 | import OrgIndex | |
| 5 | import Testing | |
| 6 | @testable import OrgApp | |
| 7 | ||
| 8 | @MainActor | |
| 9 | struct CompletionSourcesTests { | |
| 10 | @Test func presetDecidesUntilSet() { | |
| 11 | let defaults = TestDefaults("orgstar-completion") | |
| 12 | #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: false, delay: 0.4, prefix: 2, fuzzy: false, words: false)) | |
| 13 | defaults.set("doom", forKey: "keymap") | |
| 14 | #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: true, delay: 0.4, prefix: 2, fuzzy: true, words: false)) | |
| 15 | defaults.set("off", forKey: "completionAuto") | |
| 16 | defaults.set("prefix", forKey: "completionStyle") | |
| 17 | #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: false, fuzzy: false)) | |
| 18 | defaults.set("mac", forKey: "keymap") | |
| 19 | defaults.set("on", forKey: "completionAuto") | |
| 20 | defaults.set(250, forKey: "completionAutoDelay") | |
| 21 | defaults.set(3, forKey: "completionAutoPrefix") | |
| 22 | defaults.set(true, forKey: "completionWords") | |
| 23 | #expect(OrgPreferences.completion(defaults) == CompletionOptions(auto: true, delay: 0.25, prefix: 3, fuzzy: false, words: true)) | |
| 24 | #expect(UserConfig.apply("[orgstar]\ncompletion-auto = \"sometimes\"\n", to: defaults) == ["config.toml: completion-auto must be one of preset, on, off"]) | |
| 25 | } | |
| 26 | ||
| 27 | @Test func answersFromTheIndexLeavingOutThisFile() async throws { | |
| 28 | let index = try IndexStore() | |
| 29 | try index.write(FileRecord(path: "/n/a.org", root: "/n", kind: .org, bytes: Array("* Here\n".utf8), mtime: 1, settings: IndexSettings())) | |
| 30 | try index.write(FileRecord(path: "/n/b.org", root: "/n", kind: .org, bytes: Array("* There\nThermal.\n".utf8), mtime: 1, settings: IndexSettings())) | |
| 31 | let targets = await CompletionSources.answer(.linkTargets, index: index, excluding: "/n/a.org") | |
| 32 | #expect(targets.items.map(\.title) == ["There"]) | |
| 33 | #expect(await CompletionSources.answer(.words("the"), index: index, excluding: nil).strings == ["There", "Thermal"]) | |
| 34 | } | |
| 35 | ||
| 36 | /// Completing `[[id:` to a heading in another file without an ID gives it one there. | |
| 37 | @Test func linkingAHeadingElsewhereGivesItAnID() throws { | |
| 38 | let notes = try Folder() | |
| 39 | let target = try notes.write("target.org", "* Intro\n* Plan\nBody\n") | |
| 40 | let here = try notes.write("here.org", "See ") | |
| 41 | let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r"))) | |
| 42 | session.open(here.path) | |
| 43 | let item = CompletionItem(title: "Plan", id: nil, path: target.path, start: 8, heading: true) | |
| 44 | #expect(CompletionSources.createID(item, session: session, newID: "plan-id") == "plan-id") | |
| 45 | #expect(try String(contentsOf: target, encoding: .utf8) == "* Intro\n* Plan\n:PROPERTIES:\n:ID: plan-id\n:END:\nBody\n") | |
| 46 | // A heading that moved is left alone. | |
| 47 | let moved = CompletionItem(title: "Plan", id: nil, path: target.path, start: 0, heading: true) | |
| 48 | #expect(CompletionSources.createID(moved, session: session, newID: "other") == nil) | |
| 49 | // The link the completion inserts. | |
| 50 | var completion = OrgCompletion.at(EditContext(revision: 0, text: "See [[id:Pl", tree: OrgParser.parse("See [[id:Pl"), selection: [11..<11]))! | |
| 51 | completion = OrgCompletion.filled(completion, with: CompletionResult(items: [item]), fuzzy: false) | |
| 52 | let edit = CompletionFlow.linkEdit(completion, completion.item("Plan")!, id: "plan-id") | |
| 53 | #expect(edit.apply(to: "See [[id:Pl") == "See [[id:plan-id][Plan]]") | |
| 54 | } | |
| 55 | } | |
Tests/OrgIndexTests/CompletionQueriesTests.swift added +49 · view
| @@ -0,0 +1,49 @@ | ||
| 1 | import OrgCore | |
| 2 | import Testing | |
| 3 | @testable import OrgIndex | |
| 4 | ||
| 5 | struct CompletionQueriesTests { | |
| 6 | func store() throws -> IndexStore { | |
| 7 | let store = try IndexStore() | |
| 8 | try store.write(record("/notes/a.org", """ | |
| 9 | :PROPERTIES: | |
| 10 | :ID: file-a | |
| 11 | :END: | |
| 12 | #+TITLE: Project notes | |
| 13 | * Alpha | |
| 14 | :PROPERTIES: | |
| 15 | :ID: alpha | |
| 16 | :Status: waiting | |
| 17 | :Status_ALL: todo "in progress" done :ETC | |
| 18 | :END: | |
| 19 | Helmet and helium. | |
| 20 | * Beta | |
| 21 | :PROPERTIES: | |
| 22 | :Status: waiting | |
| 23 | :END: | |
| 24 | """)) | |
| 25 | try store.write(record("/notes/b.org", "* Gamma\n:PROPERTIES:\n:Status: blocked\n:END:\nHelpful words.\n")) | |
| 26 | return store | |
| 27 | } | |
| 28 | ||
| 29 | @Test func linkTargetsAreEveryHeadingWithItsID() throws { | |
| 30 | let targets = try store().linkTargets() | |
| 31 | #expect(targets.map(\.title) == ["Project notes", "Alpha", "Beta", "Gamma"]) | |
| 32 | #expect(targets[0] == CompletionItem(title: "Project notes", id: "file-a", path: "/notes/a.org", start: 0, heading: false)) | |
| 33 | #expect(targets[1].id == "alpha" && targets[2].id == nil && targets[2].heading) | |
| 34 | #expect(try store().linkTargets(excluding: "/notes/a.org").map(\.title) == ["Gamma"]) | |
| 35 | } | |
| 36 | ||
| 37 | @Test func propertyValuesMostUsedFirstAndAllowed() throws { | |
| 38 | let values = try store().propertyValues("status") | |
| 39 | #expect(values.used == ["waiting", "blocked"]) | |
| 40 | #expect(values.allowed == ["todo", "in progress", "done"]) | |
| 41 | #expect(try store().propertyValues("Status", excluding: "/notes/a.org").used == ["blocked"]) | |
| 42 | } | |
| 43 | ||
| 44 | @Test func wordsFromTheFullTextIndex() throws { | |
| 45 | #expect(try store().words("hel") == ["Helmet", "helium", "Helpful"]) | |
| 46 | #expect(try store().words("Hel", excluding: "/notes/b.org") == ["Helmet", "helium"]) | |
| 47 | #expect(try store().words("--").isEmpty) | |
| 48 | } | |
| 49 | } | |
docs/manual/guide/02-the-editor.org +35 −3 · view
| @@ -237,7 +237,7 @@ Complete at Point (=completion-at-point=) completes the word before the caret fr | ||
| 237 | 237 | | Preset | Key | |
| 238 | 238 | |--------+----------------------------------------------------------| |
| 239 | 239 | | Emacs | =C-M-i= | |
| 240 | | Doom | =C-SPC= or =C-@= in insert state; the list also opens by itself after you type two characters and pause for 0.4 seconds, as Doom's corfu does | | |
| 240 | | Doom | =C-SPC= or =C-@= in insert state | | |
| 241 | 241 | | Mac | =⌥⎋= (Option-Escape) while Option is Meta; =⌃Space= stays with macOS, which switches input sources with it | |
| 242 | 242 | |
| 243 | 243 | With one candidate, it is inserted. With several, a list opens under the caret, showing up to 16 rows. Nothing is selected until you pick a row. |
| @@ -251,7 +251,39 @@ With one candidate, it is inserted. With several, a list opens under the caret, | ||
| 251 | 251 | | =ESC= | Close the list; the key then does what it otherwise would | |
| 252 | 252 | | =C-g= | Close the list | |
| 253 | 253 | |
| 254 | The list follows what you type and closes when nothing matches. Candidates are those that start with what you typed. | |
| 254 | The list follows what you type and closes when nothing matches. | |
| 255 | ||
| 256 | ** Opening by itself | |
| 257 | ||
| 258 | The list can open by itself, as corfu's =corfu-auto= does: after you type at least two characters and pause for 0.4 seconds, when the position offers something. With the Doom keys it does, as Doom's corfu does; with the Emacs and Mac keys it doesn't. Settings ▸ Editing ▸ Completion (=completion-auto=, =completion-auto-delay= and =completion-auto-prefix= in =config.toml=) changes all three for any keys; see [[file:13-configuration.org][Configuration]]. With the Doom keys it opens only in insert state. | |
| 259 | ||
| 260 | ** Matching | |
| 261 | ||
| 262 | Candidates start with what you typed, sorted; this is Emacs's default (=completion-styles= with =basic= and =partial-completion=). With fuzzy matching (=completion-style = "fuzzy"=), a candidate matches when it has the letters you typed in order, anywhere, as the command palette matches, and the best matches come first: letters at the start of a word and runs of letters next to each other count most, then shorter candidates. Candidates in the case you typed come first when there are any, so =#+ti= lists =title:= rather than =TITLE:=. The Doom keys match fuzzily by default, as Doom's corfu does with orderless; the Emacs and Mac keys match prefixes. =TAB= inserts the part the candidates share only when it starts with what you typed. | |
| 263 | ||
| 264 | ** From your folders | |
| 265 | ||
| 266 | Some positions offer what is in your other files too. The file's own candidates show at once and the rest join the list when the index answers, which doesn't hold up typing. | |
| 267 | ||
| 268 | | Where | Candidates | Inserted | | |
| 269 | |----------------------------------------------+----------------------------------------------------------------+----------| | |
| 270 | | After =[[id:= | Heading titles in every folder, and files with an =ID=; a title that several have shows its file's name | =[[id:ID][Title]]=, through =]]= after the caret | | |
| 271 | | After =[[denote:= | Denote's notes, by title | =[[denote:IDENTIFIER][Title]]= | | |
| 272 | | After =:NAME: = in a property drawer | The values the =NAME_ALL= property or =#+PROPERTY: NAME_ALL= allows at the caret, then the values headings give =NAME= in this file and in every folder, most used first | The value | | |
| 273 | ||
| 274 | A heading chosen after =[[id:= that has no =ID= gets one first, in this file or in its own, as =org-id-get-create= adds it, so the link always points at an =ID=. If the heading's file changed so that the heading isn't where the index last saw it, nothing is added and the echo area says so. | |
| 275 | ||
| 276 | ** Words | |
| 277 | ||
| 278 | Expand Word (=dabbrev-expand=) completes the word before the caret from words that start with it, ignoring case: in the file, those before the caret nearest first, then those after it nearest first; then in the other open files; then in your folders through the index. Each word is offered once. The case follows what you typed: =he= expands to =hello= from =Hello=, =HE= to =HELLO=, and a word with capitals inside, such as =fooBar=, keeps its own case when you typed its start as it is. Pressing the key again right away replaces the word with the next one; past the last, what you typed comes back and the echo area says =No further dynamic expansion for `he' found=. | |
| 279 | ||
| 280 | | Preset | Expand Word | Complete Word | | |
| 281 | |--------+-------------+------------------------------------------------------------------| | |
| 282 | | Emacs | =M-/= | =C-M-/= | | |
| 283 | | Doom | — | =C-n= in insert state (first chosen), =C-p= (last chosen), as Doom's =+corfu/dabbrev-or-next= | | |
| 284 | | Mac | =F5= (=fn-F5= on keyboards whose top row controls the Mac) | — | | |
| 285 | ||
| 286 | Complete Word lists the same words in the completion list instead, with one chosen. Word characters are Org's: letters, digits and =$=, =%=, ='=, =&=, =*=, =+=, =-=, =/=, ~=~, =\=, =_=, =|= and =~=, so =fo= expands to =fo-y= or =fo/v= but not into =fo.z=. The list that opens by itself offers words only where Org offers nothing, and only with Settings ▸ Editing ▸ Completion ▸ The list offers words from open files and folders (=completion-words=). | |
| 255 | 287 | |
| 256 | 288 | What is completed, by position: |
| 257 | 289 | |
| @@ -273,7 +305,7 @@ What is completed, by position: | ||
| 273 | 305 | | =:= at the start of a line in a property drawer | property names the entry doesn't have yet | |
| 274 | 306 | | =:= at the start of a line elsewhere | drawer names | |
| 275 | 307 | |
| 276 | Where none of these offers anything, the triggers of your snippets that start with the text before the caret are offered, each with its description and marked =snippet=; choosing one expands it (see [[*Snippets][Snippets]]). Elsewhere, Complete at Point reports =No match=. Completion is for org files only. | |
| 308 | Where none of these offers anything, the triggers of your snippets that match the text before the caret are offered, each with its description and marked =snippet=; choosing one expands it (see [[*Snippets][Snippets]]). Elsewhere, Complete at Point reports =No match=. Completion is for org files only. | |
| 277 | 309 | |
| 278 | 310 | * Snippets |
| 279 | 311 | |
docs/manual/guide/14-ios.org +4 · view
| @@ -220,6 +220,10 @@ With a hardware keyboard, the editor uses the preset =keymap= sets in the folder | ||
| 220 | 220 | |
| 221 | 221 | See [[file:03-keys.org][Keys and commands]] for the Emacs bindings. |
| 222 | 222 | |
| 223 | ** Completion | |
| 224 | ||
| 225 | With a hardware keyboard, Complete at Point (=C-M-i=, or =C-SPC= in Doom's insert state), Expand Word (=M-/=, or =F5= with the Mac keys) and Complete Word (=C-M-/=, or =C-n= and =C-p= in Doom's insert state) work as on the Mac; see [[file:02-the-editor.org][The editor]]. The list shows up to 8 rows under the caret; the keys move through it as on the Mac, and tapping a row inserts it. The settings in Settings ▸ Completion are the Mac's: whether the list opens by itself, after which pause and how many characters, prefix or fuzzy matching, and words in the list. With them on, the list also opens as you type on the on-screen keyboard, where tapping a row is the way to choose. | |
| 226 | ||
| 223 | 227 | ** Snippets |
| 224 | 228 | |
| 225 | 229 | Snippets come from =snippets.toml= in the configuration folder, the same file as on the Mac, and reload when it changes; problems show in red under the folder in Settings. See [[file:02-the-editor.org][The editor]] and [[file:13-configuration.org][Configuration]]. |
docs/manual/guide/15-alongside-emacs.org +11 · view
| @@ -223,6 +223,17 @@ orgstar's snippets (see [[file:02-the-editor.org][The editor]]) read yasnippet's | ||
| 223 | 223 | - A field inside another field's default is skipped by yasnippet once the outer field is typed over; orgstar visits it, empty, where it was. |
| 224 | 224 | - Capture's escapes for dates and prompts (=%t=, =%U=, =%<…>=, =%^{…}=) work in bodies, which yasnippet doesn't have. |
| 225 | 225 | |
| 226 | ** Completion and dabbrev, corfu and org-roam | |
| 227 | ||
| 228 | Completion follows Emacs's =completion-at-point= with corfu's list (see [[file:02-the-editor.org][The editor]]). Where it differs: | |
| 229 | ||
| 230 | - Expand Word is =dabbrev-expand= with its defaults, and is tested against it in a buffer of its own. It searches the other open files whatever their mode, rather than only buffers in the same mode, and after them the index of your folders, which Emacs doesn't have. Without a word right before the caret it reports =No possible abbreviation preceding point= instead of expanding the word before the blank, and =dabbrev-abbrev-skip-leading-regexp= and =dabbrev-case-fold-search= aren't read. | |
| 231 | - Complete Word (and Doom's =C-n= and =C-p=) lists words in the order Expand Word finds them; =cape-dabbrev= collects the same words, which corfu then sorts by length and history. | |
| 232 | - Fuzzy matching scores as the command palette does, which is close to Doom's orderless with =orderless-flex= but takes no separators: spaces in what's typed are ignored. | |
| 233 | - The list that opens by itself reads one pause and one length for every position (=corfu-auto-delay=, =corfu-auto-prefix=), with Doom's macOS values as defaults (0.4 seconds, two characters). | |
| 234 | - =[[id:= completes every heading in your folders, as =org-roam-node-insert= completes nodes, but a heading without an =ID= gets one when chosen, which org-roam leaves to =org-id-get-create=. The description is the heading's title as written. org-roam's =roam:= links aren't completed. | |
| 235 | - Property values come from the index as well as the buffer; Org's =org-read-property-value= offers only the buffer's. | |
| 236 | ||
| 226 | 237 | ** Known differences from Emacs Org |
| 227 | 238 | |
| 228 | 239 | orgstar refuses some things with a message rather than doing something different from Emacs. These are the ones found in the current version. |