Skip to content

Commit

Permalink
Extend spacemacs/copy-file command with extra actions
Browse files Browse the repository at this point in the history
This commit implements an alternative for PR #8974
  • Loading branch information
dalanicolai committed May 9, 2021
1 parent 8adebd9 commit 76948b8
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions layers/+spacemacs/spacemacs-defaults/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -1350,10 +1350,43 @@ the right."
(interactive)
(set-buffer-file-coding-system 'undecided-dos nil))

(defun spacemacs/copy-file ()
"Write the file under new name."
(interactive)
(call-interactively 'write-file))
(defun spacemacs/copy-file (arg)
"Write the file under new name and select action on new file.
This command prompts for a new filename then prompts for
selecting an action to perform on the new file.
If prefixed with the universal ARG \\[universal-argument] the
prompt includes the buffer file name.
When prompted for selecting an action, pressing `?' will open the
help menu that explains the different options."
(interactive "P")
(let* ((new-file-name (read-file-name "Save file as: "
(if arg buffer-file-name default-directory))))
;; when selecting a directory name, add (buffer-name) or message error for
;; non-visiting-file buffers
(when (directory-name-p new-file-name)
(if buffer-file-name
(setq new-file-name (concat new-file-name (buffer-name)))
(user-error "Copying of non-visiting-file buffers requires to specify new file name.")))
(when (and (file-exists-p new-file-name)
(not (y-or-n-p (format "File %s exists. Overwrite?: " new-file-name))))
(user-error "Canceled"))
(pcase
(read-answer "New file action: "
'(("replace" ?r "open new file in current buffer")
("new-buffer" ?n "open new file in new buffer")
("only-write" ?o "write new-file only. Don't open")
("quit" ?q "exit")))
("quit" (print "Canceled"))
(code (let ((dir (file-name-directory new-file-name)))
(unless (file-exists-p dir)
(make-directory dir t)))
(if (string= "replace" code)
(write-file new-file-name)
(write-region (point-min) (point-max) new-file-name)
(when (string= "new-buffer" code)
(find-file new-file-name)))))))

;; from https://www.emacswiki.org/emacs/CopyingWholeLines
(defun spacemacs/duplicate-line-or-region (&optional n)
Expand Down

0 comments on commit 76948b8

Please sign in to comment.