diff options
| -rw-r--r-- | command.go | 23 | ||||
| -rw-r--r-- | editor.go | 20 |
2 files changed, 40 insertions, 3 deletions
@@ -34,7 +34,7 @@ func (ch *Command) IsValidCommand(cmd string) bool { // 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 } @@ -43,6 +43,11 @@ func (ch *Command) IsValidCommand(cmd string) bool { return true } + // Valid if it starts with w (write with filename) + if strings.HasPrefix(cmd, "w ") { + return true + } + // Everything else is considered invalid (will show "Command not found" message) return false } @@ -104,7 +109,10 @@ func (ch *Command) Handle(cmd string) { 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 @@ func (ch *Command) Handle(cmd string) { 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 @@ func (ch *Command) quit(force bool) { } // 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. @@ -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 @@ func (e *Editor) LoadFromReader(filename string, r io.Reader) error { e.activeBufferIndex = len(e.buffers) - 1 } + e.introDismissed = true return nil } |
