1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-26 07:13:24 +03:00

GH-255 Add Save and Save As menu items

This commit is contained in:
Tae Won Ha 2016-08-20 19:02:16 +02:00
parent b7f9642bc5
commit 85d17cfb73
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 85 additions and 0 deletions

View File

@ -129,6 +129,10 @@ public class NeoVimView: NSView, NSUserInterfaceValidations {
// MARK: - API
extension NeoVimView {
public func currentBuffer() -> NeoVimBuffer {
return self.agent.buffers().filter { $0.current }.first!
}
public func hasDirtyDocs() -> Bool {
return self.agent.hasDirtyDocs()
@ -157,10 +161,27 @@ extension NeoVimView {
public func openInNewTab(urls urls: [NSURL]) {
urls.forEach { self.open($0, cmd: "tabe") }
}
public func openInCurrentTab(url url: NSURL) {
self.open(url, cmd: "e")
}
public func closeCurrentTab() {
self.exec(command: "q")
}
public func saveCurrentTab() {
self.exec(command: "w")
}
public func saveCurrentTab(url url: NSURL) {
guard let path = url.path else {
return
}
let escapedFileName = self.agent.escapedFileNames([path])[0]
self.exec(command: "w \(escapedFileName)")
}
public func closeCurrentTabWithoutSaving() {
self.exec(command: "q!")

View File

@ -87,6 +87,16 @@
<action selector="openDocument:" target="-1" id="zbe-h0-oBM"/>
</connections>
</menuItem>
<menuItem title="Save..." keyEquivalent="s" id="Pgi-pt-dWC">
<connections>
<action selector="saveDocument:" target="-1" id="Qhz-Mg-Anr"/>
</connections>
</menuItem>
<menuItem title="Save As…" keyEquivalent="S" id="01H-3e-ZCV">
<connections>
<action selector="saveDocumentAs:" target="-1" id="2iM-rb-3fJ"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="t9v-Ee-SaA"/>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<connections>

View File

@ -110,6 +110,60 @@ extension MainWindowComponent {
self.neoVimView.open(urls: panel.URLs)
}
}
@IBAction func saveDocument(sender: AnyObject!) {
let curBuf = self.neoVimView.currentBuffer()
if curBuf.fileName == nil {
self.savePanelSheet { self.neoVimView.saveCurrentTab(url: $0) }
return
}
self.neoVimView.saveCurrentTab()
}
@IBAction func saveDocumentAs(sender: AnyObject!) {
self.savePanelSheet { url in
self.neoVimView.saveCurrentTab(url: url)
if self.neoVimView.isCurrentBufferDirty() {
self.neoVimView.openInNewTab(urls: [url])
} else {
self.neoVimView.openInCurrentTab(url: url)
}
}
}
private func savePanelSheet(action action: (NSURL) -> Void) {
let panel = NSSavePanel()
panel.beginSheetModalForWindow(self.window) { result in
guard result == NSFileHandlingPanelOKButton else {
return
}
let showAlert: () -> Void = {
let alert = NSAlert()
alert.addButtonWithTitle("OK")
alert.messageText = "Invalid File Name"
alert.informativeText = "The file name you have entered cannot be used. Please use a different name."
alert.alertStyle = .WarningAlertStyle
alert.runModal()
}
guard let url = panel.URL else {
showAlert()
return
}
guard url.path != nil else {
showAlert()
return
}
action(url)
}
}
}
// MARK: - Font Menu Items