emacs-utils.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2018 Mark H Weaver <mhw@netris.org>
  3. ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
  4. ;;; Copyright © 2018, 2020, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;; Copyright © 2019 Liliana Marie Prikler <liliana.prikler@gmail.com>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix build emacs-utils)
  22. #:use-module (guix build utils)
  23. #:use-module (ice-9 format)
  24. #:export (%emacs
  25. emacs-batch-eval
  26. emacs-batch-edit-file
  27. emacs-batch-disable-compilation
  28. emacs-generate-autoloads
  29. emacs-byte-compile-directory
  30. as-display
  31. emacs-substitute-sexps
  32. emacs-substitute-variables))
  33. ;;; Commentary:
  34. ;;;
  35. ;;; Tools to programmatically edit files using Emacs,
  36. ;;; e.g. to replace entire s-expressions in elisp files.
  37. ;;;
  38. ;;; Code:
  39. (define %emacs
  40. ;; The `emacs' command.
  41. (make-parameter "emacs"))
  42. (define (expr->string expr)
  43. "Converts EXPR, an expression, into a string."
  44. (if (string? expr)
  45. expr
  46. (format #f "~s" expr)))
  47. (define* (emacs-batch-eval expr #:key dynamic?)
  48. "Run Emacs in batch mode, and execute the Elisp code EXPR. If DYNAMIC? is
  49. true, evaluate using dynamic scoping."
  50. (invoke (%emacs) "--quick" "--batch"
  51. (format #f "--eval=(eval '~a ~:[t~;nil~])"
  52. (expr->string expr) dynamic?)))
  53. (define (emacs-batch-edit-file file expr)
  54. "Load FILE in Emacs using batch mode, and execute the elisp code EXPR."
  55. (invoke (%emacs) "--quick" "--batch"
  56. (string-append "--visit=" file)
  57. (string-append "--eval=" (expr->string expr))))
  58. (define (emacs-batch-disable-compilation file)
  59. (emacs-batch-edit-file file
  60. '(progn
  61. (add-file-local-variable 'no-byte-compile t)
  62. (basic-save-buffer))))
  63. (define (emacs-generate-autoloads name directory)
  64. "Generate autoloads for Emacs package NAME placed in DIRECTORY."
  65. (let* ((file (string-append directory "/" name "-autoloads.el"))
  66. (expr `(let ((backup-inhibited t)
  67. (generated-autoload-file ,file))
  68. (update-directory-autoloads ,directory))))
  69. (emacs-batch-eval expr #:dynamic? #t)))
  70. (define* (emacs-byte-compile-directory dir)
  71. "Byte compile all files in DIR and its sub-directories."
  72. (let ((expr `(progn
  73. (setq byte-compile-debug t) ; for proper exit status
  74. (byte-recompile-directory (file-name-as-directory ,dir) 0 1))))
  75. (emacs-batch-eval expr)))
  76. (define as-display ;syntactic keyword for 'emacs-substitute-sexps'
  77. '(as display))
  78. (define-syntax replacement-helper
  79. (syntax-rules (as-display)
  80. ((_ (leading-regexp replacement (as-display)))
  81. `(progn (goto-char (point-min))
  82. (re-search-forward ,leading-regexp)
  83. (kill-sexp)
  84. (insert " ")
  85. (insert ,(format #f "~a" replacement))))
  86. ((_ (leading-regexp replacement))
  87. `(progn (goto-char (point-min))
  88. (re-search-forward ,leading-regexp)
  89. (kill-sexp)
  90. (insert " ")
  91. (insert ,(format #f "~s" replacement))))))
  92. (define-syntax emacs-substitute-sexps
  93. (syntax-rules ()
  94. "Substitute the S-expression immediately following the first occurrence of
  95. LEADING-REGEXP by the string returned by REPLACEMENT in FILE. For example:
  96. (emacs-substitute-sexps \"w3m.el\"
  97. (\"defcustom w3m-command\"
  98. (string-append w3m \"/bin/w3m\"))
  99. (\"defvar w3m-image-viewer\"
  100. (string-append imagemagick \"/bin/display\")))
  101. This replaces the default values of the `w3m-command' and `w3m-image-viewer'
  102. variables declared in `w3m.el' with the results of the `string-append' calls
  103. above. Note that LEADING-REGEXP uses Emacs regexp syntax.
  104. Here is another example that uses the '(as-display)' subform to avoid having
  105. the Elisp procedure symbol from being double quoted:
  106. (emacs-substitute-sexps \"gnugo.el\"
  107. (\"defvar gnugo-xpms\" \"#'gnugo-imgen-create-xpms\" (as-display))"
  108. ((_ file replacement-spec ...)
  109. (emacs-batch-edit-file file
  110. `(progn ,(replacement-helper replacement-spec)
  111. ...
  112. (basic-save-buffer))))))
  113. (define-syntax emacs-substitute-variables
  114. (syntax-rules ()
  115. "Substitute the default value of VARIABLE by the string returned by
  116. REPLACEMENT in FILE. For example:
  117. (emacs-substitute-variables \"w3m.el\"
  118. (\"w3m-command\" (string-append w3m \"/bin/w3m\"))
  119. (\"w3m-image-viewer\" (string-append imagemagick \"/bin/display\")))
  120. This replaces the default values of the `w3m-command' and `w3m-image-viewer'
  121. variables declared in `w3m.el' with the results of the `string-append' calls
  122. above. Similarly to `emacs-substitute-sexps', the '(as-display)' subform can
  123. be used to have the replacement formatted like `display' would, which can be
  124. useful to avoid double quotes being added when the replacement is provided as
  125. a string."
  126. ((_ file (variable replacement modifier ...) ...)
  127. (emacs-substitute-sexps file
  128. ((string-append "(def[a-z]+[[:space:]\n]+" variable "\\>")
  129. replacement
  130. modifier ...)
  131. ...))))
  132. ;;; emacs-utils.scm ends here