From 0e365f5ede5b519a2a5cd23e86a04506bf1036f1 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Fri, 26 Apr 2024 13:26:48 +0100 Subject: [PATCH 1/2] Added file rename command --- lib/howl/buffer.moon | 9 +++++++++ lib/howl/commands/file_commands.moon | 30 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/howl/buffer.moon b/lib/howl/buffer.moon index 5496995e5..159c395e2 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' From 021c4bb6d30c831704594e63d31b4fc21985acbf Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Fri, 26 Apr 2024 13:43:12 +0100 Subject: [PATCH 2/2] Added file delete command --- lib/howl/buffer.moon | 9 ++++++++- lib/howl/commands/file_commands.moon | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/howl/buffer.moon b/lib/howl/buffer.moon index 159c395e2..a84d08559 100644 --- a/lib/howl/buffer.moon +++ b/lib/howl/buffer.moon @@ -248,7 +248,7 @@ class Buffer extends PropertyObject @_associate_with_file file @save! - rename: (file) => + rename_file: (file) => old_file = @file @_associate_with_file file @save! @@ -257,6 +257,13 @@ class Buffer extends PropertyObject if not status log.warn "Failed to delete old alias #{old_file} for #{@file}: #{err}" + delete_file: (file) => + if @file + status, err = pcall @file\delete + if not status + log.warn "Failed to delete #{@file}: #{err}" + @_associate_with_file nil + 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 23e6058ea..b9dd64976 100644 --- a/lib/howl/commands/file_commands.moon +++ b/lib/howl/commands/file_commands.moon @@ -173,11 +173,32 @@ command.register command.run 'save-as' return - buffer\rename file + buffer\rename_file 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: 'delete', + description: 'Delete the file associated with the current buffer' + handler: -> + buffer = app.editor.buffer + if not buffer.file + log.info "Not deleting; buffer not associated with a file" + return + + if buffer.modified_on_disk + overwrite = interact.yes_or_no + prompt: "Buffer '#{buffer}' has changed on disk, delete anyway? " + default: false + unless overwrite + log.info "Not deleting; buffer not saved" + return + + old_name = buffer.file.basename + buffer\delete_file! + log.info ("%s has been deleted")\format old_name + command.register name: 'buffer-close', description: 'Close the current buffer'