Sources/Orgstar/LinkOpener.swift

801e496807e41f08001dbd99622e2fa686a34058
orgstar/Sources/Orgstar/LinkOpener.swift

41 lines · 1999 bytes

 1import AppKit
 2import OrgApp
 3import OrgCore
 4
 5/// Follows a link, handing addresses and files Orgstar doesn't edit to macOS.
 6@MainActor
 7enum LinkOpener {
 8    /// Returns a message when the link can't be followed.
 9    /// `ask` picks among a query link's matches; `elisp` runs an `elisp:` link's form;
10    /// `citation` shows a citation's entry.
11    static func open(_ link: OrgLink, session: DocumentSession, workspace: WorkspaceModel, ask: @escaping (Prompt, @escaping (String?) -> Void) -> Void,
12                     elisp: ((String) -> Void)? = nil, citation: (([String]) -> Void)? = nil) -> String? {
13        #if APPSTORE
14        // A file outside the sidebar's folders needs access to its folder first.
15        if link.type == "file", !link.path.isEmpty,
16           !GrantAccess.reach([DocumentSession.resolve(link.path, from: session.path)], workspace: workspace) { return nil }
17        #endif
18        switch session.follow(link, workspace: workspace, isText: FileTypes.isText) {
19        case .shown: return nil
20        case .openExternally(let url):
21            NSWorkspace.shared.open(url)
22            return nil
23        case .message(let message): return message
24        case .choose(let prompt, let paths):
25            let labels = paths.map { ($0 as NSString).lastPathComponent }
26            ask(Prompt(key: "file", message: prompt, choices: labels)) { answer in
27                guard let answer, let index = labels.firstIndex(of: answer) else { return }
28                _ = open(OrgLink(type: "file", path: paths[index], raw: "file:" + paths[index]), session: session, workspace: workspace, ask: ask)
29            }
30            return nil
31        case .elisp(let form):
32            guard let elisp else { return "orgstar doesn't run elisp links here" }
33            elisp(form)
34            return nil
35        case .citation(let keys):
36            guard let citation else { return "orgstar doesn't follow citations here" }
37            citation(keys)
38            return nil
39        }
40    }
41}
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