1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 03:25:03 +03:00

Use paste API

This commit is contained in:
Tae Won Ha 2020-08-12 18:50:11 +02:00
parent ccda659cb1
commit c9ba804ca4
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -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<Bool> 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<Bool> in
switch self.mode {
case .insert:
let cmd = element.column == 0 ? "<ESC>\"+Pa" : "<ESC>\"+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)
}