Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rename and delete commands #613

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/howl/buffer.moon
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ class Buffer extends PropertyObject
@_associate_with_file file
@save!

rename_file: (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}"

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: =>
Expand Down
51 changes: 51 additions & 0 deletions lib/howl/commands/file_commands.moon
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,57 @@ 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 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'
Expand Down