Sources/OrgstarMobile/MobileEditor+ISearch.swift

design-themes-widgets
orgstar/Sources/OrgstarMobile/MobileEditor+ISearch.swift history · blame · raw

81 lines · 2922 bytes

 1#if os(iOS)
 2import OrgCore
 3import UIKit
 4
 5// Incremental search: C-s and C-r with a hardware keyboard in the Emacs preset, or from
 6// Commands. While it runs, typed keys go to the search and the match is selected.
 7extension MobileEditor {
 8    func startISearch(forward: Bool, regexp: Bool = false) {
 9        isearch = ISearch(origin: textView.selectedRange.location, forward: forward, previous: Self.lastISearch, regexp: regexp)
10        showISearch()
11    }
12
13    /// A key while searching: true when the search took it, false when the search ended and
14    /// the key should run as usual.
15    func isearchKey(_ chord: KeyChord) -> Bool {
16        guard var search = isearch else { return false }
17        let text = document.text
18        switch (chord.key, chord.modifiers) {
19        case ("s", [.control]), ("s", [.control, .meta]): search.again(forward: true, in: text)
20        case ("r", [.control]), ("r", [.control, .meta]): search.again(forward: false, in: text)
21        case ("DEL", []): search.delete()
22        case ("g", [.control]):
23            if search.abort() {
24                isearch = search
25                showISearch()
26            } else {
27                endISearch(caret: search.origin)
28                onMessage?("Quit")
29            }
30            return true
31        case ("RET", []):
32            finishISearch(search)
33            return true
34        case ("SPC", []): search.add(" ", in: text)
35        case (let key, let modifiers) where key.count == 1 && (modifiers.isEmpty || modifiers == [.shift]):
36            search.add(key, in: text)
37        default:
38            finishISearch(search)
39            return false
40        }
41        isearch = search
42        showISearch()
43        return true
44    }
45
46    /// Text from the on-screen keyboard while searching: it goes into the search string.
47    func isearchTyped(_ typed: String) {
48        guard var search = isearch else { return }
49        if typed == "\n" { return finishISearch(search) }
50        if typed.isEmpty {
51            search.delete()
52        } else {
53            search.add(typed, in: document.text)
54        }
55        isearch = search
56        showISearch()
57    }
58
59    func finishISearch(_ search: ISearch) {
60        if !search.string.isEmpty { Self.lastISearch = search.string }
61        endISearch(caret: search.point)
62    }
63
64    private func endISearch(caret: Int) {
65        isearch = nil
66        textView.selectedRange = NSRange(location: caret, length: 0)
67    }
68
69    private func showISearch() {
70        guard let search = isearch else { return }
71        if let match = search.match {
72            reveal(match.lowerBound)
73            textView.selectedRange = NSRange(location: match.lowerBound, length: match.count)
74            textView.scrollRangeToVisible(textView.selectedRange)
75        } else {
76            textView.selectedRange = NSRange(location: search.point, length: 0)
77        }
78        onMessage?(search.message)
79    }
80}
81#endif