guix-guile.el 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; guix-guile.el --- Auxiliary tools for working with Guile code -*- lexical-binding: t -*-
  2. ;; Copyright © 2015, 2017 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. (defvar guix-guile-definition-regexp
  22. (rx bol "(define"
  23. (zero-or-one "*")
  24. (zero-or-one "-public")
  25. (one-or-more space)
  26. (zero-or-one "(")
  27. (group (one-or-more (or word (syntax symbol)))))
  28. "Regexp used to find the guile definition.")
  29. (defun guix-guile-current-definition ()
  30. "Return string with name of the current top-level guile definition."
  31. (save-excursion
  32. (beginning-of-defun)
  33. (if (looking-at guix-guile-definition-regexp)
  34. (match-string-no-properties 1)
  35. (error "Couldn't find the current definition"))))
  36. (defun guix-guile-current-module ()
  37. "Return a string with the current guile module.
  38. Return nil, if current buffer does not define a module."
  39. ;; Modified version of `geiser-guile--get-module'.
  40. (save-excursion
  41. (geiser-syntax--pop-to-top)
  42. (when (or (re-search-backward geiser-guile--module-re nil t)
  43. (looking-at geiser-guile--library-re)
  44. (re-search-forward geiser-guile--module-re nil t))
  45. (match-string-no-properties 1))))
  46. (defun guix-guile-boolean (arg)
  47. "Return a string with guile boolean value.
  48. Transform elisp ARG (nil or non-nil) to the guile boolean (#f or #t)."
  49. (if arg "#t" "#f"))
  50. (defun guix-guile-keyword-regexp (keyword)
  51. "Return regexp to find guile KEYWORD."
  52. (format "(\\(%s\\)\\_>" keyword))
  53. (defun guix-guile-make-call-expression (proc &rest args)
  54. "Return \"(PROC ARGS ...)\" string.
  55. PROC and ARGS should be strings."
  56. (format "(%s %s)"
  57. proc
  58. (mapconcat #'identity args " ")))
  59. (defun guix-make-guile-expression (fun &rest args)
  60. "Return string containing a guile expression for calling FUN with ARGS."
  61. (format "(%S %s)" fun
  62. (mapconcat
  63. (lambda (arg)
  64. (cond
  65. ((null arg) "'()")
  66. ((stringp arg)
  67. (prin1-to-string (substring-no-properties arg)))
  68. ((or (eq arg t)
  69. ;; An ugly hack to separate 'false' from nil.
  70. (equal arg 'f)
  71. (keywordp arg))
  72. (concat "#" (prin1-to-string arg t)))
  73. ((or (symbolp arg) (listp arg))
  74. (concat "'" (prin1-to-string arg)))
  75. (t (prin1-to-string arg))))
  76. args
  77. " ")))
  78. (defun guix-guile-prompt? (string)
  79. "Return non-nil, if STRING contains a Guile prompt."
  80. (or (string-match-p geiser-guile--prompt-regexp string)
  81. (string-match-p geiser-guile--debugger-prompt-regexp string)))
  82. (provide 'guix-guile)
  83. ;;; guix-guile.el ends here