indent-code.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. :;exec emacs --batch --quick --load="$0" --funcall=main "$@"
  2. ;;; indent-code.el --- Run Emacs to indent a package definition.
  3. ;; Copyright © 2017 Alex Kost <alezost@gmail.com>
  4. ;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
  5. ;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  6. ;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
  7. ;; This file is part of GNU Guix.
  8. ;; GNU Guix is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Guix is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This scripts indents the given file or package definition in the specified
  20. ;; file using Emacs.
  21. ;;; Code:
  22. ;; Load Scheme indentation rules from ".dir-locals.el".
  23. (with-temp-buffer
  24. (scheme-mode)
  25. (let ((default-directory (file-name-as-directory load-file-name))
  26. (enable-local-variables :all))
  27. (hack-dir-local-variables)
  28. (hack-local-variables-apply)))
  29. ;; Add indentation info for Scheme constructs that are not Guix-specific.
  30. ;; This is normally provided by Geiser but this file is for people who may not
  31. ;; be running Geiser, so we just copy it here (from 'geiser-syntax.el').
  32. (defmacro guix-syntax--scheme-indent (&rest pairs)
  33. `(progn ,@(mapcar (lambda (p)
  34. `(put ',(car p) 'scheme-indent-function ',(cadr p)))
  35. pairs)))
  36. (guix-syntax--scheme-indent
  37. (and-let* 1)
  38. (case-lambda 0)
  39. (catch defun)
  40. (class defun)
  41. (dynamic-wind 0)
  42. (guard 1)
  43. (let*-values 1)
  44. (let-values 1)
  45. (let/ec 1)
  46. (letrec* 1)
  47. (match 1)
  48. (match-lambda 0)
  49. (match-lambda* 0)
  50. (match-let scheme-let-indent)
  51. (match-let* 1)
  52. (match-letrec 1)
  53. (opt-lambda 1)
  54. (parameterize 1)
  55. (parameterize* 1)
  56. (receive 2)
  57. (require-extension 0)
  58. (syntax-case 2)
  59. (test-approximate 1)
  60. (test-assert 1)
  61. (test-eq 1)
  62. (test-equal 1)
  63. (test-eqv 1)
  64. (test-group-with-cleanup 1)
  65. (test-runner-on-bad-count! 1)
  66. (test-runner-on-bad-end-name! 1)
  67. (test-runner-on-final! 1)
  68. (test-runner-on-group-begin! 1)
  69. (test-runner-on-group-end! 1)
  70. (test-runner-on-test-begin! 1)
  71. (test-runner-on-test-end! 1)
  72. (test-with-runner 1)
  73. (unless 1)
  74. (when 1)
  75. (while 1)
  76. (with-exception-handler 1)
  77. (with-syntax 1))
  78. (defun main ()
  79. (pcase command-line-args-left
  80. (`(,file-name ,package-name)
  81. ;; Indent the definition of PACKAGE-NAME in FILE-NAME.
  82. (find-file file-name)
  83. (goto-char (point-min))
  84. (if (re-search-forward (concat "^(define\\(\\|-public\\) +"
  85. package-name)
  86. nil t)
  87. (let ((indent-tabs-mode nil))
  88. (beginning-of-defun)
  89. (mark-sexp)
  90. (untabify (point) (mark))
  91. (indent-sexp)
  92. (save-buffer)
  93. (message "Done!"))
  94. (error "Package '%s' not found in '%s'"
  95. package-name file-name)))
  96. (`(,file-name)
  97. ;; Indent all of FILE-NAME.
  98. (find-file file-name)
  99. (let ((indent-tabs-mode nil))
  100. (untabify (point-min) (point-max))
  101. (indent-region (point-min) (point-max))
  102. (save-buffer)
  103. (message "Done!")))
  104. (x
  105. (error "Usage: indent-code.el FILE [PACKAGE]"))))
  106. ;;; indent-code.el ends here