diff --git a/SwiftNeoVim/NeoVimView.swift b/SwiftNeoVim/NeoVimView.swift index bc74d486..9b7dc6e8 100644 --- a/SwiftNeoVim/NeoVimView.swift +++ b/SwiftNeoVim/NeoVimView.swift @@ -166,8 +166,7 @@ public class NeoVimView: NSView, NSUserInterfaceValidations { @IBAction public func debug1(_ sender: AnyObject!) { NSLog("DEBUG 1 - Start") - NSLog("\(self.agent.vimCommandOutput("silent echo $PATH"))") - NSLog("\(self.agent.vimCommandOutput("silent pwd"))") + NSLog("\(self.agent.buffers())") NSLog("DEBUG 1 - End") } } @@ -185,9 +184,12 @@ extension NeoVimView { self.needsDisplay = true self.resizeNeoVimUiTo(size: self.bounds.size) } - - public func currentBuffer() -> NeoVimBuffer { - return self.agent.buffers().filter { $0.current }.first! + + /** + - returns: nil when for exampls a quickfix panel is open. + */ + public func currentBuffer() -> NeoVimBuffer? { + return self.agent.buffers().filter { $0.current }.first } public func hasDirtyDocs() -> Bool { @@ -195,7 +197,8 @@ extension NeoVimView { } public func isCurrentBufferDirty() -> Bool { - return self.agent.buffers().filter { $0.current }.first?.dirty ?? true + let curBuf = self.currentBuffer() + return curBuf?.dirty ?? true } public func newTab() { diff --git a/VimR/MainWindowComponent.swift b/VimR/MainWindowComponent.swift index b57fcdde..9cc3cdb8 100644 --- a/VimR/MainWindowComponent.swift +++ b/VimR/MainWindowComponent.swift @@ -222,7 +222,9 @@ extension MainWindowComponent { } @IBAction func saveDocument(_ sender: Any?) { - let curBuf = self.neoVimView.currentBuffer() + guard let curBuf = self.neoVimView.currentBuffer() else { + return + } if curBuf.fileName == nil { self.savePanelSheet { self.neoVimView.saveCurrentTab(url: $0) } @@ -233,6 +235,10 @@ extension MainWindowComponent { } @IBAction func saveDocumentAs(_ sender: Any?) { + if self.neoVimView.currentBuffer() == nil { + return + } + self.savePanelSheet { url in self.neoVimView.saveCurrentTab(url: url) @@ -396,14 +402,34 @@ extension MainWindowComponent { extension MainWindowComponent { public func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { - guard item.action == #selector(focusNeoVimView(_:)) else { + let canSave = self.neoVimView.currentBuffer() != nil + let canSaveAs = canSave + let canOpen = canSave + let canOpenQuickly = canSave + let canFocusNeoVimView = self.window.firstResponder != self.neoVimView + + guard let action = item.action else { return true } - if self.window.firstResponder == self.neoVimView { - return false - } + switch action { + case #selector(focusNeoVimView(_:)): + return canFocusNeoVimView - return true + case #selector(openDocument(_:)): + return canOpen + + case #selector(openQuickly(_:)): + return canOpenQuickly + + case #selector(saveDocument(_:)): + return canSave + + case #selector(saveDocumentAs(_:)): + return canSaveAs + + default: + return true + } } }