file-defuns.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;; Code:
  2. ;; Defuns for working with files
  3. (defun distopico:move-file (new-location)
  4. "Write this file to NEW-LOCATION, and delete the old one."
  5. (interactive (list (if buffer-file-name
  6. (read-file-name "Move file to: ")
  7. (read-file-name "Move file to: "
  8. default-directory
  9. (expand-file-name (file-name-nondirectory (buffer-name))
  10. default-directory)))))
  11. (when (file-exists-p new-location)
  12. (delete-file new-location))
  13. (let ((old-location (buffer-file-name)))
  14. (write-file new-location t)
  15. (when (and old-location
  16. (file-exists-p new-location))
  17. (delete-file old-location))))
  18. (defun distopico:locate-parent-file (regexp)
  19. "Locate a file in current o parent path with a file matching REGEXP."
  20. (locate-dominating-file
  21. default-directory
  22. (lambda (directory)
  23. (> (length (directory-files directory nil regexp t)) 0))))
  24. (defun distopico:rename-current-buffer-file ()
  25. "Renames current buffer and file it is visiting."
  26. (interactive)
  27. (let ((name (buffer-name))
  28. (filename (buffer-file-name)))
  29. (if (not (and filename (file-exists-p filename)))
  30. (error "Buffer '%s' is not visiting a file!" name)
  31. (let ((new-name (read-file-name "New name: " filename)))
  32. (if (get-buffer new-name)
  33. (error "A buffer named '%s' already exists!" new-name)
  34. (rename-file filename new-name 1)
  35. (rename-buffer new-name)
  36. (set-visited-file-name new-name)
  37. (set-buffer-modified-p nil)
  38. (message "File '%s' successfully renamed to '%s'"
  39. name (file-name-nondirectory new-name)))))))
  40. (defun distopico:delete-current-buffer-file ()
  41. "Remove file connected to current buffer and kill buffer."
  42. (interactive)
  43. (let ((filename (buffer-file-name))
  44. (buffer (current-buffer))
  45. (name (buffer-name)))
  46. (if (not (and filename (file-exists-p filename)))
  47. (ido-kill-buffer)
  48. (when (yes-or-no-p "Are you sure you want to remove this file? ")
  49. (delete-file filename)
  50. (kill-buffer buffer)
  51. (message "File '%s' successfully removed" filename)))))
  52. (defun distopico:copy-current-file-path ()
  53. "Add current file path to kill ring, limits the filename to project root if possible."
  54. (interactive)
  55. (let ((filename (buffer-file-name)))
  56. (kill-new (if eproject-mode
  57. (s-chop-prefix (eproject-root) filename)
  58. filename))))
  59. (defun distopico:find-or-create-file-at-point ()
  60. "Guess what parts of the buffer under point is a file name and opens it."
  61. (interactive)
  62. (find-file (file-name-at-point)))
  63. (defun distopico:find-or-create-file-at-point-other-window ()
  64. "Guess what parts of the buffer under point is a file name and opens it."
  65. (interactive)
  66. (find-file-other-window (file-name-at-point)))
  67. (defun distopico:file-name-at-point ()
  68. (save-excursion
  69. (let* ((file-name-regexp "[./a-zA-Z0-9\-_~]")
  70. (start (progn
  71. (while (looking-back file-name-regexp)
  72. (forward-char -1))
  73. (point)))
  74. (end (progn
  75. (while (looking-at file-name-regexp)
  76. (forward-char 1))
  77. (point))))
  78. (buffer-substring start end))))
  79. (defun distopico:touch-buffer-file ()
  80. "Create new file."
  81. (interactive)
  82. (insert " ")
  83. (backward-delete-char 1)
  84. (save-buffer))
  85. (provide 'file-defuns)