guix-guile.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ;;; guix-guile.el --- Auxiliary tools for working with Guile code -*- lexical-binding: t -*-
  2. ;; Copyright © 2015, 2017, 2018 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides functions for parsing guile code, making guile
  18. ;; expressions, etc.
  19. ;;; Code:
  20. (require 'geiser-guile)
  21. (require 'guix-utils)
  22. (defvar guix-guile-definition-regexp
  23. (rx bol "(define"
  24. (zero-or-one "*")
  25. (zero-or-one "-public")
  26. (one-or-more space)
  27. (zero-or-one "(")
  28. (group (one-or-more (or word (syntax symbol)))))
  29. "Regexp used to find the guile definition.")
  30. (defun guix-guile-current-definition ()
  31. "Return string with name of the current top-level guile definition."
  32. (save-excursion
  33. ;; If the point is in the beginning of the current definition,
  34. ;; `beginning-of-defun' will move it to the previous one, so we
  35. ;; narrow to the definition at first (there is also a commentary
  36. ;; about this problem in `narrow-to-defun').
  37. (narrow-to-defun)
  38. (beginning-of-defun)
  39. (if (looking-at guix-guile-definition-regexp)
  40. (progn
  41. (widen)
  42. (match-string-no-properties 1))
  43. (widen)
  44. (error "Couldn't find the current definition"))))
  45. (defun guix-guile-current-module ()
  46. "Return a string with the current guile module.
  47. Return nil, if current buffer does not define a module."
  48. ;; Modified version of `geiser-guile--get-module'.
  49. (save-excursion
  50. (geiser-syntax--pop-to-top)
  51. (when (or (re-search-backward geiser-guile--module-re nil t)
  52. (looking-at geiser-guile--library-re)
  53. (re-search-forward geiser-guile--module-re nil t))
  54. (match-string-no-properties 1))))
  55. (defun guix-guile-boolean (arg)
  56. "Return a string with guile boolean value.
  57. Transform elisp ARG (nil or non-nil) to the guile boolean (#f or #t)."
  58. (if arg "#t" "#f"))
  59. (defun guix-guile-keyword-regexp (keyword)
  60. "Return regexp to find guile KEYWORD."
  61. (format "(\\(%s\\)\\_>" keyword))
  62. (defun guix-guile-make-call-expression (proc &rest args)
  63. "Return \"(PROC ARGS ...)\" string.
  64. PROC and ARGS should be strings."
  65. (format "(%s %s)"
  66. proc
  67. (mapconcat #'identity args " ")))
  68. (defun guix-make-guile-expression (fun &rest args)
  69. "Return string containing a guile expression for calling FUN with ARGS."
  70. (format "(%S %s)" fun
  71. (mapconcat
  72. (lambda (arg)
  73. (cond
  74. ((null arg) "'()")
  75. ((stringp arg)
  76. (prin1-to-string (substring-no-properties arg)))
  77. ((or (eq arg t)
  78. ;; An ugly hack to separate 'false' from nil.
  79. (equal arg 'f)
  80. (keywordp arg))
  81. (concat "#" (prin1-to-string arg t)))
  82. ((or (symbolp arg) (listp arg))
  83. (concat "'" (prin1-to-string arg)))
  84. (t (prin1-to-string arg))))
  85. args
  86. " ")))
  87. (defun guix-guile-prompt? (string)
  88. "Return non-nil, if STRING contains a Guile prompt."
  89. (or (string-match-p geiser-guile--prompt-regexp string)
  90. (string-match-p geiser-guile--debugger-prompt-regexp string)))
  91. (defun guix-guile-read ()
  92. "Read guile code from the current buffer and 'transform' it into elisp.
  93. The contents of the current buffer may be modified."
  94. (goto-char (point-min))
  95. (cond
  96. ((or (looking-at-p "^#f")
  97. (looking-at-p "^#<unspecified>"))
  98. nil)
  99. ((looking-at-p "^#t")
  100. t)
  101. (t
  102. (guix-replace-match "[ (]\\(#f\\)" "nil" 1)
  103. (guix-replace-match "[ (]\\(#t\\)" "t" 1)
  104. (read (current-buffer)))))
  105. (defun guix-guile-read-from-file (file-name)
  106. "Read guile code from FILE-NAME and 'transform' it into elisp."
  107. (with-temp-buffer
  108. (insert-file-contents file-name)
  109. (guix-guile-read)))
  110. (defun guix-guile-read-from-string (string)
  111. "Read guile code from string and 'transform' it into elisp."
  112. (with-temp-buffer
  113. (insert string)
  114. (guix-guile-read)))
  115. (provide 'guix-guile)
  116. ;;; guix-guile.el ends here