file functions
This commit is contained in:
parent
973e784a3d
commit
209204b7b6
1 changed files with 87 additions and 0 deletions
87
init.el
87
init.el
|
@ -208,6 +208,9 @@
|
||||||
"ff" 'counsel-find-file
|
"ff" 'counsel-find-file
|
||||||
"fL" 'counsel-locate
|
"fL" 'counsel-locate
|
||||||
"fr" 'counsel-recentf
|
"fr" 'counsel-recentf
|
||||||
|
"fR" 'rename-current-buffer-file
|
||||||
|
"fD" 'delete-current-buffer-file
|
||||||
|
"fc" 'copy-file'
|
||||||
|
|
||||||
;; Git
|
;; Git
|
||||||
"g" '(:ignore t :which-key "Magit")
|
"g" '(:ignore t :which-key "Magit")
|
||||||
|
@ -308,6 +311,90 @@ if prefix argument ARG is given, switch to it in an other, possibly new window."
|
||||||
(interactive)
|
(interactive)
|
||||||
(switch-to-buffer nil))
|
(switch-to-buffer nil))
|
||||||
|
|
||||||
|
(defun delete-current-buffer-file ()
|
||||||
|
"Remove file connected to current buffer and kill buffer."
|
||||||
|
(interactive)
|
||||||
|
(let ((filename (buffer-file-name))
|
||||||
|
(buffer (current-buffer))
|
||||||
|
(name (buffer-name)))
|
||||||
|
(if (not (and filename (file-exists-p filename)))
|
||||||
|
(ido-kill-buffer)
|
||||||
|
(if (yes-or-no-p
|
||||||
|
(format "Are you sure you want to delete this file: '%s'?" name))
|
||||||
|
(progn
|
||||||
|
(delete-file filename t)
|
||||||
|
(kill-buffer buffer)
|
||||||
|
(when (projectile-project-p)
|
||||||
|
(call-interactively #'projectile-invalidate-cache))
|
||||||
|
(message "File deleted: '%s'" filename))
|
||||||
|
(message "Canceled: File deletion")))))
|
||||||
|
|
||||||
|
(defun rename-current-buffer-file (&optional arg)
|
||||||
|
"Rename the current buffer and the file it is visiting.
|
||||||
|
If the buffer isn't visiting a file, ask if it should
|
||||||
|
be saved to a file, or just renamed.
|
||||||
|
|
||||||
|
If called without a prefix argument (ARG), the prompt is
|
||||||
|
initialized with the current directory instead of filename."
|
||||||
|
(interactive "P")
|
||||||
|
(let* ((name (buffer-name))
|
||||||
|
(filename (buffer-file-name)))
|
||||||
|
(if (and filename (file-exists-p filename))
|
||||||
|
;; the buffer is visiting a file
|
||||||
|
(let* ((dir (file-name-directory filename))
|
||||||
|
(new-name (read-file-name "New name: " (if arg dir filename))))
|
||||||
|
(cond ((get-buffer new-name)
|
||||||
|
(error "A buffer named '%s' already exists!" new-name))
|
||||||
|
(t
|
||||||
|
(let ((dir (file-name-directory new-name)))
|
||||||
|
(when (and (not (file-exists-p dir))
|
||||||
|
(yes-or-no-p
|
||||||
|
(format "Create directory '%s'?" dir)))
|
||||||
|
(make-directory dir t)))
|
||||||
|
(rename-file filename new-name 1)
|
||||||
|
(rename-buffer new-name)
|
||||||
|
(set-visited-file-name new-name)
|
||||||
|
(set-buffer-modified-p nil)
|
||||||
|
(when (fboundp 'recentf-add-file)
|
||||||
|
(recentf-add-file new-name)
|
||||||
|
(recentf-remove-if-non-kept filename))
|
||||||
|
(when (projectile-project-p)
|
||||||
|
(call-interactively #'projectile-invalidate-cache))
|
||||||
|
(message "File '%s' successfully renamed to '%s'"
|
||||||
|
name (file-name-nondirectory new-name)))))
|
||||||
|
;; the buffer is not visiting a file
|
||||||
|
(let ((key))
|
||||||
|
(while (not (memq key '(?s ?r)))
|
||||||
|
(setq key (read-key (propertize
|
||||||
|
(format
|
||||||
|
(concat "Buffer '%s' is not visiting a file: "
|
||||||
|
"[s]ave to file or [r]ename buffer?")
|
||||||
|
name)
|
||||||
|
'face 'minibuffer-prompt)))
|
||||||
|
(cond ((eq key ?s) ; save to file
|
||||||
|
;; this allows for saving a new empty (unmodified) buffer
|
||||||
|
(unless (buffer-modified-p) (set-buffer-modified-p t))
|
||||||
|
(save-buffer))
|
||||||
|
((eq key ?r) ; rename buffer
|
||||||
|
(let ((new-name (read-string "New buffer name: ")))
|
||||||
|
(while (get-buffer new-name)
|
||||||
|
;; ask to rename again, if the new buffer name exists
|
||||||
|
(if (yes-or-no-p
|
||||||
|
(format (concat "A buffer named '%s' already exists: "
|
||||||
|
"Rename again?")
|
||||||
|
new-name))
|
||||||
|
(setq new-name (read-string "New buffer name: "))
|
||||||
|
(keyboard-quit)))
|
||||||
|
(rename-buffer new-name)
|
||||||
|
(message "Buffer '%s' successfully renamed to '%s'"
|
||||||
|
name new-name)))
|
||||||
|
;; ?\a = C-g, ?\e = Esc and C-[
|
||||||
|
((memq key '(?\a ?\e)) (keyboard-quit))))))))
|
||||||
|
|
||||||
|
(defun copy-file ()
|
||||||
|
"Write the file under new name."
|
||||||
|
(interactive)
|
||||||
|
(call-interactively 'write-file))
|
||||||
|
|
||||||
;; other
|
;; other
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue