al-backup.el 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ;;; al-backup.el --- Auxiliary code for backup files
  2. ;; Copyright © 2012-2016 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. ;; Setting `make-backup-file-name-function' is not enough as it is used
  15. ;; by `make-backup-file-name', but not by `find-backup-file-name', so
  16. ;; replace `make-backup-file-name-1' instead.
  17. (defun al/make-backup-file-name-1 (file)
  18. "Return a new backup file path of a given FILE.
  19. If the new path's directories do not exist, create them.
  20. This function is intended to be used as a substitution for
  21. `make-backup-file-name-1'."
  22. (let ((alist backup-directory-alist)
  23. (file (expand-file-name file))
  24. elt backup-directory abs-backup-directory backup-file)
  25. (while alist
  26. (setq elt (pop alist))
  27. (if (string-match (car elt) file)
  28. (setq backup-directory (cdr elt)
  29. alist nil)))
  30. (if (null backup-directory)
  31. (setq backup-file file)
  32. (setq backup-file
  33. ;; New full path in backup dir tree.
  34. (concat (directory-file-name (expand-file-name backup-directory))
  35. file)
  36. abs-backup-directory (file-name-directory backup-file))
  37. (if (and abs-backup-directory
  38. (not (file-exists-p abs-backup-directory)))
  39. (condition-case nil
  40. (make-directory abs-backup-directory 'parents)
  41. (file-error (setq backup-directory nil
  42. abs-backup-directory nil)))))
  43. backup-file))
  44. (defun al/backup-enable-predicate (name)
  45. "Function for `backup-enable-predicate'.
  46. Do not backup su/sudo files."
  47. (and (normal-backup-enable-predicate name)
  48. (not (let ((method (file-remote-p name 'method)))
  49. (when (stringp method)
  50. (member method '("su" "sudo")))))))
  51. (provide 'al-backup)
  52. ;;; al-backup.el ends here