file-defuns.el 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;; Defuns for working with files
  2. (defun rename-current-buffer-file ()
  3. "Renames current buffer and file it is visiting."
  4. (interactive)
  5. (let ((name (buffer-name))
  6. (filename (buffer-file-name)))
  7. (if (not (and filename (file-exists-p filename)))
  8. (error "Buffer '%s' is not visiting a file!" name)
  9. (let ((new-name (read-file-name "New name: " filename)))
  10. (if (get-buffer new-name)
  11. (error "A buffer named '%s' already exists!" new-name)
  12. (rename-file filename new-name 1)
  13. (rename-buffer new-name)
  14. (set-visited-file-name new-name)
  15. (set-buffer-modified-p nil)
  16. (message "File '%s' successfully renamed to '%s'"
  17. name (file-name-nondirectory new-name)))))))
  18. (defun delete-current-buffer-file ()
  19. "Removes file connected to current buffer and kills buffer."
  20. (interactive)
  21. (let ((filename (buffer-file-name))
  22. (buffer (current-buffer))
  23. (name (buffer-name)))
  24. (if (not (and filename (file-exists-p filename)))
  25. (ido-kill-buffer)
  26. (when (yes-or-no-p "Are you sure you want to remove this file? ")
  27. (delete-file filename)
  28. (kill-buffer buffer)
  29. (message "File '%s' successfully removed" filename)))))
  30. (defun copy-current-file-path ()
  31. "Add current file path to kill ring. Limits the filename to project root if possible."
  32. (interactive)
  33. (let ((filename (buffer-file-name)))
  34. (kill-new (if eproject-mode
  35. (s-chop-prefix (eproject-root) filename)
  36. filename))))
  37. (defun find-or-create-file-at-point ()
  38. "Guesses what parts of the buffer under point is a file name and opens it."
  39. (interactive)
  40. (find-file (file-name-at-point)))
  41. (defun find-or-create-file-at-point-other-window ()
  42. "Guesses what parts of the buffer under point is a file name and opens it."
  43. (interactive)
  44. (find-file-other-window (file-name-at-point)))
  45. (defun file-name-at-point ()
  46. (save-excursion
  47. (let* ((file-name-regexp "[./a-zA-Z0-9\-_~]")
  48. (start (progn
  49. (while (looking-back file-name-regexp)
  50. (forward-char -1))
  51. (point)))
  52. (end (progn
  53. (while (looking-at file-name-regexp)
  54. (forward-char 1))
  55. (point))))
  56. (buffer-substring start end))))
  57. (defun touch-buffer-file ()
  58. (interactive)
  59. (insert " ")
  60. (backward-delete-char 1)
  61. (save-buffer))
  62. (provide 'file-defuns)