reindent.el 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;;; reindent.el
  2. ;;; a reindenting script to automagically indent files.
  3. ;;; Licenced under the GNU GPLv3 or later.
  4. ;;; Copyright © 2014, Samuel W. Flint <swflint@flintfam.org>
  5. (require 'find-lisp)
  6. (defun indent-buffer ()
  7. "Indent Current Buffer"
  8. (indent-region (point-min) (point-max)))
  9. (defun indent-correctly ()
  10. "Indent the current buffer, unless the region is active"
  11. (if (region-active-p)
  12. (progn
  13. (indent-region
  14. (region-beginning)
  15. (region-end))
  16. (message "Indented Region."))
  17. (indent-buffer)
  18. (message "Indented Buffer.")))
  19. (defun untabify-correctly ()
  20. "Untabify current buffer, unless the region is active"
  21. (if (region-active-p)
  22. (progn
  23. (untabify (region-beginning) (region-end))
  24. (message "Untabified Region"))
  25. (untabify (point-min) (point-max))
  26. (message "Untabified Buffer")))
  27. (defun indent-driver ()
  28. (indent-correctly)
  29. (untabify-correctly)
  30. (save-buffer))
  31. (defun indent-driver-with-file (file-path)
  32. (let ((file-buffer (find-file file-path)))
  33. (with-current-buffer file-buffer
  34. (indent-driver)
  35. (save-buffer))
  36. (kill-buffer file-buffer)))
  37. (defun reindent-directory-contents (directory regex)
  38. "Reindent the files in a directory.
  39. Map `indent-driver-with-file' over files in DIRECTORY whose names
  40. match REGEX. Uses `find-lisp-find-files' to locate files to map
  41. over.
  42. Returns 't'."
  43. (interactive (list (read-directory-name "Directory: ")
  44. (read-regexp "Matching Regexp: ")))
  45. (mapc #'indent-driver-with-file (find-lisp-find-files directory regex))
  46. t)
  47. (provide 'reindent-tools)
  48. ;;;; end reindent.el