#if os(macOS) import AppKit import OrgCore import OrgDocument /// Modal editing: keys go to the keymap for the current evil state first (leader keys, /// evil-org's org keys), then to `Vim`. extension OrgEditor { func handleModal(_ chord: KeyChord) -> Bool { // ⌘ shortcuts stay with the menus. if chord.modifiers.contains(.command) { return false } if recordingRegister != nil, replayingMacro == 0 { recordedKeys.append(chord) } dispatcher.mode = vim.mode.keymapMode if !vim.pending.isEmpty, vim.mode != .insert { return feedVim([chord]) } switch dispatcher.feed(chord) { case .pending(let keys): let next = keymap.continuations(of: keys, mode: dispatcher.mode).map { item in (key: item.key.description, title: item.command.map { EditorCommands.command($0)?.title ?? $0 } ?? "+prefix") } onKeysPending?(KeySequence.format(keys), next) return true case .cancelled: onKeysPending?(nil, []) return true case .complete(let keys, let candidates): if keys.count > 1 { onKeysPending?(nil, []) } let context = editContext() if let binding = candidates.first(where: { binding in (binding.when.map { KeyContext.holds($0, in: context) } ?? true) && commandApplies(binding.command, in: context) }) { run(binding.command) updateCursor() return true } return feedVim(keys) case .undefined(let keys): onKeysPending?(nil, []) return feedVim(keys) case .unbound: return feedVim([chord]) } } /// Plays keys into the engine. False hands a single key in insert state to the text /// system. func feedVim(_ keys: [KeyChord]) -> Bool { for (index, key) in keys.enumerated() { if vim.mode == .insert, vim.passesThrough(key) { if keys.count == 1 { return false } insertTyped(key) continue } let leaving = key == KeyChord("ESC") || key == KeyChord("[", .control) || key == KeyChord("g", .control) if vim.mode == .insert, leaving, let snapshot = insertSnapshot { // What was typed since insert state began, for `.`. if let edit = TextEdit.between(snapshot.text, document.text).first, edit.range.count == 0 { vim.inserted(edit.replacement) } } let wasInsert = vim.mode == .insert let visual = vim.mode == .visual || vim.mode == .visualLine let caret = visual ? (visualCaret ?? textView.selectedRange().location) : textView.selectedRange().location let clipboard = NSPasteboard.general.string(forType: .string) var marks = vimMarks if jumpIndex > 0, jumpIndex <= jumpList.count { marks["'"] = jumpList[jumpIndex - 1] } let actions = vim.feed(key, Vim.Context(text: document.text, caret: caret, clipboard: clipboard, halfPage: halfPage, marks: marks)) apply(actions) if !wasInsert, vim.mode == .insert { insertSnapshot = (document.text, textView.selectedRange().location) } else if vim.mode != .insert { insertSnapshot = nil } _ = index } updateCursor() return true } /// Moves marks and jump positions through an edit. func mapVimPositions(through edit: TextEdit) { guard !vimMarks.isEmpty || !jumpList.isEmpty else { return } let names = Array(vimMarks.keys) let mapped = ViewState(selection: names.map { vimMarks[$0]!.. EditContext { let selected = textView.selectedRange() return EditContext(revision: document.revision, text: document.text, tree: document.tree, selection: [selected.location.. Bool { if !isOrg, id.hasPrefix("org.") { return false } return switch EditorCommands.command(id)?.action { case .text(let command): command.applies(in: context) default: true } } func apply(_ actions: [Vim.Action]) { for action in actions { switch action { case .edit(let edits, let caret): visualCaret = nil guard textView.isEditable else { onMessage?("This file is read-only.") continue } applyEdits(edits) setCaret(caret) textView.scrollRangeToVisible(NSRange(location: min(caret, document.text.utf16.count), length: 0)) case .move(let caret): setCaret(caret) visualCaret = caret textView.scrollRangeToVisible(NSRange(location: caret, length: 0)) case .select(let range): textView.setSelectedRange(NSRange(range)) case .command(let id): run(id) case .undo(let count): for _ in 0.. 100 { jumpList.removeFirst(jumpList.count - 100) } jumpIndex = jumpList.count case .jumpBack(let count): // Leaving the newest place keeps it, so C-i comes back to it. if jumpIndex == jumpList.count { jumpList.append(textView.selectedRange().location) jumpIndex = jumpList.count - 1 } guard jumpIndex - count >= 0 else { continue } jumpIndex -= count reveal(jumpList[jumpIndex]) case .jumpForward(let count): guard jumpIndex + count < jumpList.count else { continue } jumpIndex += count reveal(jumpList[jumpIndex]) case .recordMacro(let name): if let name { recordingRegister = name recordedKeys = [] onMessage?("Defining keyboard macro…") } else if let register = recordingRegister { // Without the `q` that ended it. macros[register] = Array(recordedKeys.dropLast()) recordingRegister = nil onMessage?("Keyboard macro defined") } case .playMacro(let name, let count): guard let keys = macros[name], !keys.isEmpty else { onMessage?("No macro in register \(name)") continue } guard replayingMacro < 20 else { continue } replayingMacro += 1 defer { replayingMacro -= 1 } for _ in 0..= end, end > line.location { setCaret(end - 1) } } /// `/`, `?` and `:` read their argument in the echo area. private func prompt(_ kind: String) { onPrompt?(Prompt(key: kind, message: kind)) { [weak self] answer in guard let self, let answer, !answer.isEmpty else { return } let caret = self.textView.selectedRange().location let context = Vim.Context(text: self.document.text, caret: caret) if kind == ":" { self.ex(answer) } else { self.apply(self.vim.search(answer, forward: kind == "/", context)) } self.updateCursor() } } /// The ex commands people reach for: `:w`, `:q`, `:wq`, `:x`, `:N`, `:s`, `:%s`, `:e FILE`, /// `:bn`, `:bp`, `:bd`, `:b` and `:ls`. func ex(_ command: String) { let trimmed = command.trimmingCharacters(in: .whitespaces) if let m = trimmed.firstMatch(of: #/^e(?:dit)?!?\s+(.+)$/#) { onOpenFile?(String(m.1).trimmingCharacters(in: .whitespaces)) return } switch trimmed { case "w", "write": onAppCommand?("app.save") case "bn", "bnext": onAppCommand?("app.buffer.next") case "bp", "bprevious", "bN", "bNext": onAppCommand?("app.buffer.previous") case "bd", "bdelete", "bd!", "bw", "bwipeout": onAppCommand?("app.buffer.kill") case "b", "buffer", "ls", "buffers", "files": onAppCommand?("app.buffer.switch") case "e", "edit", "e!", "edit!": onAppCommand?("app.revert") case "q", "quit", "q!": textView.window?.performClose(nil) case "wq", "x", "wq!": onAppCommand?("app.save") textView.window?.performClose(nil) case "noh", "nohlsearch": break default: if let line = Int(trimmed) { let ns = document.text as NSString var offset = 0 for _ in 1..