123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- ;;; Code:
- ;; Defuns for working with files
- (defun distopico:rename-current-buffer-file ()
- "Renames current buffer and file it is visiting."
- (interactive)
- (let ((name (buffer-name))
- (filename (buffer-file-name)))
- (if (not (and filename (file-exists-p filename)))
- (error "Buffer '%s' is not visiting a file!" name)
- (let ((new-name (read-file-name "New name: " filename)))
- (if (get-buffer new-name)
- (error "A buffer named '%s' already exists!" new-name)
- (rename-file filename new-name 1)
- (rename-buffer new-name)
- (set-visited-file-name new-name)
- (set-buffer-modified-p nil)
- (message "File '%s' successfully renamed to '%s'"
- name (file-name-nondirectory new-name)))))))
- (defun distopico:move-file (new-location)
- "Write this file to NEW-LOCATION, and delete the old one."
- (interactive (list (if buffer-file-name
- (read-file-name "Move file to: ")
- (read-file-name "Move file to: "
- default-directory
- (expand-file-name (file-name-nondirectory (buffer-name))
- default-directory)))))
- (when (file-exists-p new-location)
- (delete-file new-location))
- (let ((old-location (buffer-file-name)))
- (write-file new-location t)
- (when (and old-location
- (file-exists-p new-location))
- (delete-file old-location))))
- (defun distopico:locate-parent-file (regexp)
- "Locate a file in current o parent path with a file matching REGEXP."
- (locate-dominating-file
- default-directory
- (lambda (directory)
- (> (length (directory-files directory nil regexp t)) 0))))
- (defun distopico:rename-current-buffer-file ()
- "Renames current buffer and file it is visiting."
- (interactive)
- (let ((name (buffer-name))
- (filename (buffer-file-name)))
- (if (not (and filename (file-exists-p filename)))
- (error "Buffer '%s' is not visiting a file!" name)
- (let ((new-name (read-file-name "New name: " filename)))
- (if (get-buffer new-name)
- (error "A buffer named '%s' already exists!" new-name)
- (rename-file filename new-name 1)
- (rename-buffer new-name)
- (set-visited-file-name new-name)
- (set-buffer-modified-p nil)
- (message "File '%s' successfully renamed to '%s'"
- name (file-name-nondirectory new-name)))))))
- (defun distopico: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)
- (when (yes-or-no-p "Are you sure you want to remove this file? ")
- (delete-file filename)
- (kill-buffer buffer)
- (message "File '%s' successfully removed" filename)))))
- (defun distopico:copy-current-file-path ()
- "Add current file path to kill ring, limits the filename to project root if possible."
- (interactive)
- (let ((filename (buffer-file-name)))
- (kill-new (if eproject-mode
- (s-chop-prefix (eproject-root) filename)
- filename))))
- (defun distopico:find-or-create-file-at-point ()
- "Guess what parts of the buffer under point is a file name and opens it."
- (interactive)
- (find-file (file-name-at-point)))
- (defun distopico:find-or-create-file-at-point-other-window ()
- "Guess what parts of the buffer under point is a file name and opens it."
- (interactive)
- (find-file-other-window (file-name-at-point)))
- (defun distopico:file-name-at-point ()
- (save-excursion
- (let* ((file-name-regexp "[./a-zA-Z0-9\-_~]")
- (start (progn
- (while (looking-back file-name-regexp)
- (forward-char -1))
- (point)))
- (end (progn
- (while (looking-at file-name-regexp)
- (forward-char 1))
- (point))))
- (buffer-substring start end))))
- (defun distopico:touch-buffer-file ()
- "Create new file."
- (interactive)
- (insert " ")
- (backward-delete-char 1)
- (save-buffer))
- (provide 'file-defuns)
|