guix-geiser.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ;;; guix-geiser.el --- Interacting with Geiser -*- lexical-binding: t -*-
  2. ;; Copyright © 2015, 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 to evaluate guile code using Geiser.
  18. ;;; Code:
  19. (require 'geiser-mode)
  20. (require 'guix-guile)
  21. (defvar guix-geiser-connection-timeout (* 3 60 1000) ; 3 minutes
  22. "Time, in milliseconds, to wait for REPL to return a result.
  23. Same as `geiser-connection-timeout' but is used for Guix
  24. commands. By default, this variable has a significant value,
  25. because some commands (like `guix-store-live-items') may take a
  26. very long time to run.")
  27. (defun guix-geiser-repl ()
  28. "Return the current Geiser REPL."
  29. (or geiser-repl--repl
  30. (geiser-repl--repl/impl 'guile)
  31. (error "Geiser REPL not found")))
  32. (defun guix-geiser-eval (str &optional repl)
  33. "Evaluate STR with guile expression using Geiser REPL.
  34. If REPL is nil, use the current Geiser REPL.
  35. Return a list of strings with result values of evaluation."
  36. (let ((gc-cons-threshold (max gc-cons-threshold 10000000)))
  37. (with-current-buffer (or repl (guix-geiser-repl))
  38. (let ((res (geiser-eval--send/wait
  39. `(:eval (:scm ,str))
  40. guix-geiser-connection-timeout)))
  41. (unless res
  42. (error "\
  43. Sorry, the evaluation is aborted because it has taken too much time.
  44. Try to increase the value of `guix-geiser-connection-timeout'
  45. variable if you have a slow machine, or please report if you
  46. think this command takes unreasonably long time to run."))
  47. (if (geiser-eval--retort-error res)
  48. (error "Error in evaluating guile expression: %s"
  49. (geiser-eval--retort-output res))
  50. (cdr (assq 'result res)))))))
  51. (defun guix-geiser-eval-read (str &optional repl)
  52. "Evaluate STR with guile expression using Geiser REPL.
  53. Return elisp expression of the first result value of evaluation."
  54. (guix-guile-read-from-string (car (guix-geiser-eval str repl))))
  55. (defun guix-geiser-eval-in-repl (str &optional repl no-history no-display)
  56. "Switch to Geiser REPL and evaluate STR with guile expression there.
  57. If NO-HISTORY is non-nil, do not save STR in the REPL history.
  58. If NO-DISPLAY is non-nil, do not switch to the REPL buffer."
  59. (let ((repl (or repl (guix-geiser-repl))))
  60. (with-current-buffer repl
  61. (geiser-repl--send str (not no-history)))
  62. (unless no-display
  63. (geiser-repl--switch-to-buffer repl))))
  64. (defun guix-geiser-eval-in-repl-synchronously (str &optional repl
  65. no-history no-display)
  66. "Evaluate STR in Geiser REPL synchronously, i.e. wait until the
  67. REPL operation will be finished.
  68. See `guix-geiser-eval-in-repl' for the meaning of arguments."
  69. (let* ((repl (if repl (get-buffer repl) (guix-geiser-repl)))
  70. (running? nil)
  71. (filter (lambda (output)
  72. (setq running?
  73. (and (get-buffer-process repl)
  74. (not (guix-guile-prompt? output))))))
  75. (comint-output-filter-functions
  76. (cons filter comint-output-filter-functions)))
  77. (guix-geiser-eval-in-repl str repl no-history no-display)
  78. (while running?
  79. (sleep-for 0.1))))
  80. (defun guix-geiser-call (proc &rest args)
  81. "Call (PROC ARGS ...) synchronously using the current Geiser REPL.
  82. PROC and ARGS should be strings."
  83. (guix-geiser-eval
  84. (apply #'guix-guile-make-call-expression proc args)))
  85. (defun guix-geiser-call-in-repl (proc &rest args)
  86. "Call (PROC ARGS ...) in the current Geiser REPL.
  87. PROC and ARGS should be strings."
  88. (guix-geiser-eval-in-repl
  89. (apply #'guix-guile-make-call-expression proc args)))
  90. (provide 'guix-geiser)
  91. ;;; guix-geiser.el ends here