indent-code.el.in 3.2 KB

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