emacs-utils.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;; Copyright © 2019 Leo Prikler <leo.prikler@student.tugraz.at>
  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. emacs-substitute-sexps
  31. emacs-substitute-variables))
  32. ;;; Commentary:
  33. ;;;
  34. ;;; Tools to programmatically edit files using Emacs,
  35. ;;; e.g. to replace entire s-expressions in elisp files.
  36. ;;;
  37. ;;; Code:
  38. (define %emacs
  39. ;; The `emacs' command.
  40. (make-parameter "emacs"))
  41. (define (expr->string expr)
  42. "Converts EXPR, an expression, into a string."
  43. (if (string? expr)
  44. expr
  45. (format #f "~s" expr)))
  46. (define* (emacs-batch-eval expr #:key dynamic?)
  47. "Run Emacs in batch mode, and execute the Elisp code EXPR. If DYNAMIC? is
  48. true, evaluate using dynamic scoping."
  49. (invoke (%emacs) "--quick" "--batch"
  50. (format #f "--eval=(eval '~a ~:[t~;nil~])"
  51. (expr->string expr) dynamic?)))
  52. (define (emacs-batch-edit-file file expr)
  53. "Load FILE in Emacs using batch mode, and execute the elisp code EXPR."
  54. (invoke (%emacs) "--quick" "--batch"
  55. (string-append "--visit=" file)
  56. (string-append "--eval=" (expr->string expr))))
  57. (define (emacs-batch-disable-compilation file)
  58. (emacs-batch-edit-file file
  59. '(progn
  60. (add-file-local-variable 'no-byte-compile t)
  61. (basic-save-buffer))))
  62. (define (emacs-generate-autoloads name directory)
  63. "Generate autoloads for Emacs package NAME placed in DIRECTORY."
  64. (let* ((file (string-append directory "/" name "-autoloads.el"))
  65. (expr `(let ((backup-inhibited t)
  66. (generated-autoload-file ,file))
  67. (update-directory-autoloads ,directory))))
  68. (emacs-batch-eval expr #:dynamic? #t)))
  69. (define* (emacs-byte-compile-directory dir)
  70. "Byte compile all files in DIR and its sub-directories."
  71. (let ((expr `(progn
  72. (setq byte-compile-debug t) ; for proper exit status
  73. (byte-recompile-directory (file-name-as-directory ,dir) 0 1))))
  74. (emacs-batch-eval expr)))
  75. (define-syntax emacs-substitute-sexps
  76. (syntax-rules ()
  77. "Substitute the S-expression immediately following the first occurrence of
  78. LEADING-REGEXP by the string returned by REPLACEMENT in FILE. For example:
  79. (emacs-substitute-sexps \"w3m.el\"
  80. (\"defcustom w3m-command\"
  81. (string-append w3m \"/bin/w3m\"))
  82. (\"defvar w3m-image-viewer\"
  83. (string-append imagemagick \"/bin/display\")))
  84. This replaces the default values of the `w3m-command' and `w3m-image-viewer'
  85. variables declared in `w3m.el' with the results of the `string-append' calls
  86. above. Note that LEADING-REGEXP uses Emacs regexp syntax."
  87. ((emacs-substitute-sexps file (leading-regexp replacement) ...)
  88. (emacs-batch-edit-file file
  89. `(progn (progn (goto-char (point-min))
  90. (re-search-forward ,leading-regexp)
  91. (kill-sexp)
  92. (insert " ")
  93. (insert ,(format #f "~S" replacement)))
  94. ...
  95. (basic-save-buffer))))))
  96. (define-syntax emacs-substitute-variables
  97. (syntax-rules ()
  98. "Substitute the default value of VARIABLE by the string returned by
  99. REPLACEMENT in FILE. For example:
  100. (emacs-substitute-variables \"w3m.el\"
  101. (\"w3m-command\" (string-append w3m \"/bin/w3m\"))
  102. (\"w3m-image-viewer\" (string-append imagemagick \"/bin/display\")))
  103. This replaces the default values of the `w3m-command' and `w3m-image-viewer'
  104. variables declared in `w3m.el' with the results of the `string-append' calls
  105. above."
  106. ((emacs-substitute-variables file (variable replacement) ...)
  107. (emacs-substitute-sexps file
  108. ((string-append "(def[a-z]+[[:space:]\n]+" variable "\\>")
  109. replacement)
  110. ...))))
  111. ;;; emacs-utils.scm ends here