emacs-utils.scm 4.5 KB

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