diff --git a/command.go b/command.go index e6fc9eb46913c1b670a854eecfe286a8481689b9..e8f453635cc74cc30f006dac810b520dbec43388 100644 --- a/command.go +++ b/command.go @@ -34,12 +34,17 @@ } // Valid if it's a known command switch cmd { - case "q", "Q", "q!", "Q!", "w", "W", "wa", "WA", "wq", "WQ", "waq", "WAQ", "reload", "bd", "bd!", "debug", "help", "mouse", "e", "edit": + case "q", "Q", "q!", "Q!", "w", "W", "wa", "WA", "wq", "WQ", "waq", "WAQ", "reload", "bd", "bd!", "debug", "help", "mouse", "e", "edit", "n": return true } // Valid if it starts with ! (shell command) or r! (read shell) if strings.HasPrefix(cmd, "!") || strings.HasPrefix(cmd, "r!") { + return true + } + + // Valid if it starts with w (write with filename) + if strings.HasPrefix(cmd, "w ") { return true } @@ -104,7 +109,10 @@ ch.quit(false) case cmd == "q!" || cmd == "Q!": ch.quit(true) case cmd == "w" || cmd == "W": - ch.write() + ch.write("") + case strings.HasPrefix(cmd, "w "): + filename := strings.TrimSpace(strings.TrimPrefix(cmd, "w ")) + ch.write(filename) case cmd == "wa" || cmd == "WA": ch.writeAll() case cmd == "wq" || cmd == "WQ": @@ -129,6 +137,8 @@ case cmd == "bd": ch.bufferDelete(false) case cmd == "bd!": ch.bufferDelete(true) + case cmd == "n": + ch.e.NewBuffer() case cmd == "debug": ch.e.toggleDebugWindow() case cmd == "help": @@ -217,7 +227,14 @@ os.Exit(0) } // write saves the current active buffer to disk. -func (ch *Command) write() { +func (ch *Command) write(filename string) { + if filename != "" { + b := ch.e.activeBuffer() + if b != nil { + b.filename = filename + b.fileType = getFileType(filename) + } + } err := ch.e.SaveFile(false) if err != nil { // Handle conflict if the file was changed externally. diff --git a/editor.go b/editor.go index f889232c5f66325a992e63d9da1421a4be30ca08..1011c094908b2d235c8d72e882792951f97aaf99 100644 --- a/editor.go +++ b/editor.go @@ -238,6 +238,25 @@ func (e *Editor) toggleDebugWindow() { e.showDebugLog = !e.showDebugLog } +// NewBuffer creates a new empty buffer and switches to it. +func (e *Editor) NewBuffer() { + // Use default file type (Text), which is the last one in the list. + defaultType := fileTypes[len(fileTypes)-1] + + newB := &Buffer{ + buffer: [][]rune{{}}, + filename: "", + undoStack: []HistoryState{}, + redoStack: []HistoryState{}, + fileType: defaultType, + } + + e.buffers = append(e.buffers, newB) + e.activeBufferIndex = len(e.buffers) - 1 + e.message = "New buffer created" + e.introDismissed = true +} + // LoadFile reads a file from disk into the active buffer. func (e *Editor) LoadFile(filename string) error { info, err := os.Stat(filename) @@ -381,6 +400,7 @@ e.buffers = append(e.buffers, newB) e.activeBufferIndex = len(e.buffers) - 1 } + e.introDismissed = true return nil }