From c9ba804ca481f9bfd1cbe065dd787476321d4d1d Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Wed, 12 Aug 2020 18:50:11 +0200 Subject: [PATCH] Use paste API --- NvimView/NvimView/NvimView+MenuItems.swift | 64 ++-------------------- 1 file changed, 5 insertions(+), 59 deletions(-) diff --git a/NvimView/NvimView/NvimView+MenuItems.swift b/NvimView/NvimView/NvimView+MenuItems.swift index a6d8efd6..f479acf2 100644 --- a/NvimView/NvimView/NvimView+MenuItems.swift +++ b/NvimView/NvimView/NvimView+MenuItems.swift @@ -117,67 +117,13 @@ extension NvimView { } @IBAction func paste(_ sender: Any?) { - guard let content = NSPasteboard.general.string(forType: .string) else { - return - } + guard let content = NSPasteboard.general.string(forType: .string) else { return } - if self.mode == .cmdlineNormal || self.mode == .cmdlineInsert || self.mode == .cmdlineReplace - || self.mode == .replace - || self.mode == .termFocus { - self.api - .input(keys: self.vimPlainString(content), errWhenBlocked: false) - .subscribe(onError: { error in - self.eventsSubject.onNext(.apiError(msg: "Could not paste \(content)", cause: error)) - }) - .disposed(by: self.disposeBag) - return - } - - Single.zip( - self.api - .getCurrentWin() - .flatMap { win in self.api.winGetCursor(window: win) }, - self.api - .getOption(name: "paste") - .flatMap { curPasteMode -> Single in - if curPasteMode == false { - return self.api - .setOption(name: "paste", value: .bool(true)) - .andThen(Single.just(true)) - } else { - return Single.just(false) - } - } - ) - .map { result in (column: result.0[1], pasteModeSet: result.1) } - .flatMap { element -> Single in - switch self.mode { - - case .insert: - let cmd = element.column == 0 ? "\"+Pa" : "\"+pa" - return self.api - .input(keys: cmd, errWhenBlocked: false).asCompletable() - .andThen(Single.just(element.pasteModeSet)) - - case .normal, .visual: - return self.api - .input(keys: "\"+p", errWhenBlocked: false).asCompletable() - .andThen(Single.just(element.pasteModeSet)) - - default: - return Single.just(element.pasteModeSet) - - } - } - .flatMapCompletable { pasteModeSet -> Completable in - if pasteModeSet { - return self.api.setOption(name: "paste", value: .bool(false)) - } - - return Completable.empty() - } + // phase == 1 means paste in a single call + self.api + .paste(data: content, crlf: false, phase: -1) .subscribe(onError: { error in - self.eventsSubject.onNext(.apiError(msg: "There was an pasting.", cause: error)) + self.eventsSubject.onNext(.apiError(msg: "Could not paste \(content)", cause: error)) }) .disposed(by: self.disposeBag) }