guix-geiser.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; guix-geiser.el --- Interacting with Geiser -*- lexical-binding: t -*-
  2. ;; Copyright © 2015 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 to evaluate guile code using Geiser.
  18. ;;; Code:
  19. (require 'geiser-mode)
  20. (require 'guix-guile)
  21. (defun guix-geiser-repl ()
  22. "Return the current Geiser REPL."
  23. (or geiser-repl--repl
  24. (geiser-repl--repl/impl 'guile)
  25. (error "Geiser REPL not found")))
  26. (defun guix-geiser-eval (str &optional repl)
  27. "Evaluate STR with guile expression using Geiser REPL.
  28. If REPL is nil, use the current Geiser REPL.
  29. Return a list of strings with result values of evaluation."
  30. (let ((gc-cons-threshold (max gc-cons-threshold 10000000)))
  31. (with-current-buffer (or repl (guix-geiser-repl))
  32. (let ((res (geiser-eval--send/wait `(:eval (:scm ,str)))))
  33. (if (geiser-eval--retort-error res)
  34. (error "Error in evaluating guile expression: %s"
  35. (geiser-eval--retort-output res))
  36. (cdr (assq 'result res)))))))
  37. (defun guix-geiser-eval-read (str &optional repl)
  38. "Evaluate STR with guile expression using Geiser REPL.
  39. Return elisp expression of the first result value of evaluation."
  40. ;; The goal is to convert a string with scheme expression into elisp
  41. ;; expression.
  42. (let ((result (car (guix-geiser-eval str repl))))
  43. (cond
  44. ((or (string= result "#f")
  45. (string= result "#<unspecified>"))
  46. nil)
  47. ((string= result "#t")
  48. t)
  49. (t
  50. (read (replace-regexp-in-string
  51. "[ (]\\(#f\\)" "nil"
  52. (replace-regexp-in-string
  53. "[ (]\\(#t\\)" "t"
  54. result
  55. nil nil 1)
  56. nil nil 1))))))
  57. (defun guix-geiser-eval-in-repl (str &optional repl no-history no-display)
  58. "Switch to Geiser REPL and evaluate STR with guile expression there.
  59. If NO-HISTORY is non-nil, do not save STR in the REPL history.
  60. If NO-DISPLAY is non-nil, do not switch to the REPL buffer."
  61. (let ((repl (or repl (guix-geiser-repl))))
  62. (with-current-buffer repl
  63. (geiser-repl--send str (not no-history)))
  64. (unless no-display
  65. (geiser-repl--switch-to-buffer repl))))
  66. (defun guix-geiser-eval-in-repl-synchronously (str &optional repl
  67. no-history no-display)
  68. "Evaluate STR in Geiser REPL synchronously, i.e. wait until the
  69. REPL operation will be finished.
  70. See `guix-geiser-eval-in-repl' for the meaning of arguments."
  71. (let* ((repl (if repl (get-buffer repl) (guix-geiser-repl)))
  72. (running? nil)
  73. (filter (lambda (output)
  74. (setq running?
  75. (and (get-buffer-process repl)
  76. (not (guix-guile-prompt? output))))))
  77. (comint-output-filter-functions
  78. (cons filter comint-output-filter-functions)))
  79. (guix-geiser-eval-in-repl str repl no-history no-display)
  80. (while running?
  81. (sleep-for 0.1))))
  82. (defun guix-geiser-call (proc &rest args)
  83. "Call (PROC ARGS ...) synchronously using the current Geiser REPL.
  84. PROC and ARGS should be strings."
  85. (guix-geiser-eval
  86. (apply #'guix-guile-make-call-expression proc args)))
  87. (defun guix-geiser-call-in-repl (proc &rest args)
  88. "Call (PROC ARGS ...) in the current Geiser REPL.
  89. PROC and ARGS should be strings."
  90. (guix-geiser-eval-in-repl
  91. (apply #'guix-guile-make-call-expression proc args)))
  92. (provide 'guix-geiser)
  93. ;;; guix-geiser.el ends here