diff --git a/lib/howl/buffer.moon b/lib/howl/buffer.moon index 022214638..aee11d340 100644 --- a/lib/howl/buffer.moon +++ b/lib/howl/buffer.moon @@ -248,6 +248,15 @@ class Buffer extends PropertyObject @_associate_with_file file @save! + rename: (file) => + old_file = @file + @_associate_with_file file + @save! + if @file != old_file + status, err = pcall old_file\delete + if not status + log.warn "Failed to delete old alias #{old_file} for #{@file}: #{err}" + as_one_undo: (f) => @_buffer\as_one_undo f undo: => diff --git a/lib/howl/commands/file_commands.moon b/lib/howl/commands/file_commands.moon index 97cd71c22..23e6058ea 100644 --- a/lib/howl/commands/file_commands.moon +++ b/lib/howl/commands/file_commands.moon @@ -148,6 +148,36 @@ command.register log.info ("%s: %d lines, %d bytes written")\format buffer.file.basename, #buffer.lines, #buffer +command.register + name: 'rename', + description: 'Rename the current buffer\'s file' + input: (opts) -> + parent = app.editor.buffer.file and app.editor.buffer.file.parent + file = interact.select_file prompt: ':rename ', text: opts.text, allow_new: true, path: parent and parent.path + return unless file + + if file.exists + unless interact.yes_or_no prompt: "File '#{file}' already exists, overwrite? " + log.info "Not overwriting; buffer not renamed" + return + + unless auto_mkdir file.parent + log.info "Parent directory doesn't exist; buffer not renamed" + return + + return file + + handler: (file) -> + buffer = app.editor.buffer + if not buffer.file + command.run 'save-as' + return + + buffer\rename file + buffer.mode = mode.for_file file + log.info ("%s: %d lines, %d bytes written")\format buffer.file.basename, + #buffer.lines, #buffer + command.register name: 'buffer-close', description: 'Close the current buffer'