file-defuns.el 4.2 KB

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