2015-06-15 09:05:52 +02:00
|
|
|
;;; defuns-file.el
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:file-delete "defuns-file" nil t)
|
|
|
|
(evil-define-command doom:file-delete (&optional bang filename)
|
2016-09-30 14:37:25 +02:00
|
|
|
"Delete current buffer's file. If BANG, kill buffer afterwards."
|
2015-06-15 09:05:52 +02:00
|
|
|
:repeat nil
|
|
|
|
(interactive "<!><f>")
|
|
|
|
(let ((filename (file-truename (or filename (buffer-file-name)))))
|
|
|
|
(if (not (file-exists-p filename))
|
|
|
|
(error "File doesn't exist: %s" filename)
|
2015-12-23 03:47:18 -05:00
|
|
|
(when (or bang (and (not bang) (y-or-n-p (format "Delete %s?" (f-base filename)))))
|
2016-03-31 03:18:03 -04:00
|
|
|
(set-buffer-modified-p nil)
|
2015-12-23 03:47:18 -05:00
|
|
|
(delete-file filename)
|
2015-06-15 09:05:52 +02:00
|
|
|
(kill-this-buffer)
|
2016-05-20 22:37:30 -04:00
|
|
|
(unless (doom/real-buffer-p)
|
|
|
|
(doom/previous-real-buffer))
|
2015-12-23 03:47:18 -05:00
|
|
|
(save-place-forget-unreadable-files)
|
|
|
|
(message "File successfully deleted: %s" filename)))))
|
2015-06-15 09:05:52 +02:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom--save-exit() (save-buffer) (kill-buffer) (remove-hook 'yas-after-exit-snippet-hook '--save-exit))
|
|
|
|
;;;###autoload (autoload 'doom:file-create "defuns-file" nil t)
|
|
|
|
(evil-define-command doom:file-create (path &optional bang)
|
2015-06-15 09:05:52 +02:00
|
|
|
"Deploy files (and their associated templates) quickly. Will prompt
|
|
|
|
you to fill in each snippet field before buffer closes unless BANG is
|
|
|
|
provided."
|
|
|
|
:repeat nil
|
|
|
|
(interactive "<f><!>")
|
|
|
|
(let ((dir (f-dirname path))
|
|
|
|
(fullpath (f-full path))
|
|
|
|
(is-auto t))
|
2016-05-06 01:57:50 -04:00
|
|
|
(when (and bang (not (f-exists? dir)))
|
|
|
|
(mkdir dir))
|
|
|
|
(if (f-exists? dir)
|
|
|
|
(if (f-exists? fullpath)
|
2015-06-15 09:05:52 +02:00
|
|
|
(error "File already exists: %s" path)
|
|
|
|
(find-file fullpath)
|
2016-05-20 22:37:30 -04:00
|
|
|
(add-hook 'yas-after-exit-snippet-hook 'doom--save-exit)
|
|
|
|
(if bang (doom--save-exit)))
|
2015-06-15 09:05:52 +02:00
|
|
|
(error "Directory doesn't exist: %s" dir))))
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:file-move "defuns-file" nil t)
|
|
|
|
(evil-define-command doom:file-move (path)
|
2015-06-15 09:05:52 +02:00
|
|
|
"Move current buffer's file to PATH. Replaces %, # and other variables (see
|
2016-09-30 14:37:25 +02:00
|
|
|
`evil-ex-replace-special-filenames')"
|
2015-06-15 09:05:52 +02:00
|
|
|
:repeat nil
|
|
|
|
(interactive "<f>")
|
|
|
|
(let* ((old-path (buffer-file-name))
|
|
|
|
(new-path (cond ((f-dir? path)
|
|
|
|
(f-expand (f-filename old-path) path))
|
|
|
|
((f-dir? (f-dirname path))
|
|
|
|
(f-full path))
|
|
|
|
(t (user-error "Not a valid destination: %s" path))))
|
2016-05-20 22:37:30 -04:00
|
|
|
(project-root (doom/project-root)))
|
2016-03-11 19:30:58 -05:00
|
|
|
;; Move all attachments if in org-mode
|
|
|
|
(when (eq major-mode 'org-mode)
|
|
|
|
(mapc (lambda (file)
|
2016-04-16 21:38:47 -04:00
|
|
|
(when (and (file-exists-p file) (not (f-same? old-path new-path)))
|
2016-03-11 19:30:58 -05:00
|
|
|
(rename-file file (f-expand (f-filename old-path) (f-dirname new-path)) t)))
|
2016-05-20 22:37:30 -04:00
|
|
|
(doom/org-attachments)))
|
2016-03-31 03:18:03 -04:00
|
|
|
(when (buffer-modified-p)
|
|
|
|
(save-buffer))
|
2015-06-15 09:05:52 +02:00
|
|
|
(rename-file old-path new-path 1)
|
|
|
|
(rename-buffer (f-filename new-path))
|
|
|
|
(set-visited-file-name new-path)
|
|
|
|
(set-buffer-modified-p nil)
|
|
|
|
(save-place-forget-unreadable-files)
|
2016-05-20 22:37:30 -04:00
|
|
|
(setq doom--spaceline-file-path nil)
|
2015-06-15 09:05:52 +02:00
|
|
|
(message "File '%s' successfully renamed to '%s'"
|
|
|
|
(f-relative old-path project-root) (f-relative new-path project-root))))
|
|
|
|
|
|
|
|
(provide 'defuns-file)
|
|
|
|
;;; defuns-file.el ends here
|