Sources/OrgEditorAppKit/OrgEditor+ISearch.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/OrgEditorAppKit/OrgEditor+ISearch.swift history · blame · raw

108 lines · 4455 bytes

6 symbols in this file
  1#if os(macOS)
  2import AppKit
  3import OrgCore
  4
  5// Incremental search: C-s and C-r in the Emacs preset, `/` and `?` in the Doom preset. While
  6// it runs, keys go to the search; the match is selected and the other matches highlighted.
  7extension OrgEditor {
  8    /// Starts searching from the caret. `vim`: `/` or `?`, which end with Vim's search, so `n`
  9    /// and `N` repeat it.
 10    func startISearch(forward: Bool, vim: Bool = false, regexp: Bool = false) {
 11        isearch = ISearch(origin: textView.selectedRange().location, forward: forward, previous: Self.lastISearch, regexp: regexp || vim)
 12        isearchVim = vim
 13        showISearch()
 14    }
 15
 16    /// A key while searching: true when the search took it, false when the search ended
 17    /// and the key should run as usual.
 18    func isearchKey(_ chord: KeyChord) -> Bool {
 19        guard var search = isearch else { return false }
 20        let text = document.text
 21        switch (chord.key, chord.modifiers) {
 22        case ("s", [.control]) where !isearchVim, ("s", [.control, .meta]) where !isearchVim:
 23            search.again(forward: true, in: text)
 24        case ("r", [.control]) where !isearchVim, ("r", [.control, .meta]) where !isearchVim:
 25            search.again(forward: false, in: text)
 26        case ("DEL", []):
 27            if isearchVim, search.string.isEmpty {
 28                endISearch(caret: search.origin)
 29                return true
 30            }
 31            search.delete()
 32        case ("g", [.control]):
 33            if search.abort() {
 34                isearch = search
 35                showISearch()
 36            } else {
 37                endISearch(caret: search.origin)
 38                onMessage?("Quit")
 39            }
 40            return true
 41        case ("ESC", []) where isearchVim:
 42            endISearch(caret: search.origin)
 43            return true
 44        case ("RET", []):
 45            finishISearch(search)
 46            return true
 47        case ("SPC", []):
 48            search.add(" ", in: text)
 49        case (let key, let modifiers) where key.count == 1 && (modifiers.isEmpty || modifiers == [.shift]):
 50            search.add(key, in: text)
 51        default:
 52            // Any other key ends the search where it is, then runs.
 53            finishISearch(search)
 54            return false
 55        }
 56        isearch = search
 57        showISearch()
 58        return true
 59    }
 60
 61    private func finishISearch(_ search: ISearch) {
 62        if !search.string.isEmpty { Self.lastISearch = search.string }
 63        if isearchVim, !search.string.isEmpty {
 64            endISearch(caret: search.origin)
 65            let context = Vim.Context(text: document.text, caret: search.origin)
 66            apply(vim.search(search.string, forward: search.state.forward, context))
 67            updateCursor()
 68        } else {
 69            endISearch(caret: search.point)
 70        }
 71    }
 72
 73    private func endISearch(caret: Int) {
 74        isearch = nil
 75        setISearchHighlights([])
 76        setCaret(caret)
 77    }
 78
 79    /// The match selected, the others highlighted, and the prompt in the echo area.
 80    private func showISearch() {
 81        guard let search = isearch else { return }
 82        if let match = search.match {
 83            // Folded text around the match opens, as `isearch-open-invisible` opens it.
 84            reveal(match.lowerBound)
 85            textView.setSelectedRange(NSRange(location: match.lowerBound, length: match.count))
 86            textView.scrollRangeToVisible(NSRange(location: match.lowerBound, length: match.count))
 87        } else {
 88            textView.setSelectedRange(NSRange(location: search.point, length: 0))
 89        }
 90        var others: [Range<Int>] = []
 91        if !search.string.isEmpty, let regex = ISearch.pattern(search.string, regexp: search.regexp) {
 92            for m in regex.matches(in: document.text, range: NSRange(location: 0, length: utf16Length)).prefix(1000) where m.range.length > 0 {
 93                let range = m.range.location..<NSMaxRange(m.range)
 94                if range != search.match { others.append(range) }
 95            }
 96        }
 97        setISearchHighlights(others)
 98        onMessage?(isearchVim ? (search.state.forward ? "/" : "?") + search.string : search.message)
 99    }
100
101    private func setISearchHighlights(_ ranges: [Range<Int>]) {
102        let old = highlights
103        highlights = ranges
104        for range in old + ranges where range.upperBound <= utf16Length { restyleOutsideEditing(range) }
105        redrawViews()
106    }
107}
108#endif