Add :w filename and :n new buffer
| Author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-22 18:51:24 +0100 |
| Committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-22 18:51:24 +0100 |
| Commit | 3d25687572aa9373e324a06413ceffba15d6bceb (patch) |
|
-rw-r--r-- |
command.go | 23 | |
-rw-r--r-- |
editor.go | 20 |
2 files changed, 40 insertions, 3 deletions
| diff --git a/command.go b/command.go | |||
| ... | |||
| 34 | 34 | ||
| 35 | // Valid if it's a known command |
35 | // Valid if it's a known command |
| 36 | switch cmd { |
36 | switch cmd { |
| 37 | case "q", "Q", "q!", "Q!", "w", "W", "wa", "WA", "wq", "WQ", "waq", "WAQ", "reload", "bd", "bd!", "debug", "help", "mouse", "e", "edit": |
37 | case "q", "Q", "q!", "Q!", "w", "W", "wa", "WA", "wq", "WQ", "waq", "WAQ", "reload", "bd", "bd!", "debug", "help", "mouse", "e", "edit", "n": |
| 38 | return true |
38 | return true |
| 39 | } |
39 | } |
| 40 | 40 | ||
| 41 | // Valid if it starts with ! (shell command) or r! (read shell) |
41 | // Valid if it starts with ! (shell command) or r! (read shell) |
| 42 | if strings.HasPrefix(cmd, "!") || strings.HasPrefix(cmd, "r!") { |
42 | if strings.HasPrefix(cmd, "!") || strings.HasPrefix(cmd, "r!") { |
| 43 | return true |
||
| 44 | } |
||
| 45 | |||
| 46 | // Valid if it starts with w (write with filename) |
||
| 47 | if strings.HasPrefix(cmd, "w ") { |
||
| 43 | return true |
48 | return true |
| 44 | } |
49 | } |
| 45 | 50 | ||
| ... | |||
| 104 | case cmd == "q!" || cmd == "Q!": |
109 | case cmd == "q!" || cmd == "Q!": |
| 105 | ch.quit(true) |
110 | ch.quit(true) |
| 106 | case cmd == "w" || cmd == "W": |
111 | case cmd == "w" || cmd == "W": |
| 107 | ch.write() |
112 | ch.write("") |
| 113 | case strings.HasPrefix(cmd, "w "): |
||
| 114 | filename := strings.TrimSpace(strings.TrimPrefix(cmd, "w ")) |
||
| 115 | ch.write(filename) |
||
| 108 | case cmd == "wa" || cmd == "WA": |
116 | case cmd == "wa" || cmd == "WA": |
| 109 | ch.writeAll() |
117 | ch.writeAll() |
| 110 | case cmd == "wq" || cmd == "WQ": |
118 | case cmd == "wq" || cmd == "WQ": |
| ... | |||
| 129 | ch.bufferDelete(false) |
137 | ch.bufferDelete(false) |
| 130 | case cmd == "bd!": |
138 | case cmd == "bd!": |
| 131 | ch.bufferDelete(true) |
139 | ch.bufferDelete(true) |
| 140 | case cmd == "n": |
||
| 141 | ch.e.NewBuffer() |
||
| 132 | case cmd == "debug": |
142 | case cmd == "debug": |
| 133 | ch.e.toggleDebugWindow() |
143 | ch.e.toggleDebugWindow() |
| 134 | case cmd == "help": |
144 | case cmd == "help": |
| ... | |||
| 217 | } |
227 | } |
| 218 | 228 | ||
| 219 | // write saves the current active buffer to disk. |
229 | // write saves the current active buffer to disk. |
| 220 | func (ch *Command) write() { |
230 | func (ch *Command) write(filename string) { |
| 231 | if filename != "" { |
||
| 232 | b := ch.e.activeBuffer() |
||
| 233 | if b != nil { |
||
| 234 | b.filename = filename |
||
| 235 | b.fileType = getFileType(filename) |
||
| 236 | } |
||
| 237 | } |
||
| 221 | err := ch.e.SaveFile(false) |
238 | err := ch.e.SaveFile(false) |
| 222 | if err != nil { |
239 | if err != nil { |
| 223 | // Handle conflict if the file was changed externally. |
240 | // Handle conflict if the file was changed externally. |
| ... | |||
| diff --git a/editor.go b/editor.go | |||
| ... | |||
| 238 | e.showDebugLog = !e.showDebugLog |
238 | e.showDebugLog = !e.showDebugLog |
| 239 | } |
239 | } |
| 240 | 240 | ||
| 241 | // NewBuffer creates a new empty buffer and switches to it. |
||
| 242 | func (e *Editor) NewBuffer() { |
||
| 243 | // Use default file type (Text), which is the last one in the list. |
||
| 244 | defaultType := fileTypes[len(fileTypes)-1] |
||
| 245 | |||
| 246 | newB := &Buffer{ |
||
| 247 | buffer: [][]rune{{}}, |
||
| 248 | filename: "", |
||
| 249 | undoStack: []HistoryState{}, |
||
| 250 | redoStack: []HistoryState{}, |
||
| 251 | fileType: defaultType, |
||
| 252 | } |
||
| 253 | |||
| 254 | e.buffers = append(e.buffers, newB) |
||
| 255 | e.activeBufferIndex = len(e.buffers) - 1 |
||
| 256 | e.message = "New buffer created" |
||
| 257 | e.introDismissed = true |
||
| 258 | } |
||
| 259 | |||
| 241 | // LoadFile reads a file from disk into the active buffer. |
260 | // LoadFile reads a file from disk into the active buffer. |
| 242 | func (e *Editor) LoadFile(filename string) error { |
261 | func (e *Editor) LoadFile(filename string) error { |
| 243 | info, err := os.Stat(filename) |
262 | info, err := os.Stat(filename) |
| ... | |||
| 381 | e.activeBufferIndex = len(e.buffers) - 1 |
400 | e.activeBufferIndex = len(e.buffers) - 1 |
| 382 | } |
401 | } |
| 383 | 402 | ||
| 403 | e.introDismissed = true |
||
| 384 | return nil |
404 | return nil |
| 385 | } |
405 | } |
| 386 | 406 | ||
| ... | |||