1 ;;; guix-geiser.el --- Interacting with Geiser -*- lexical-binding: t -*-
3 ;; Copyright © 2015 Alex Kost <alezost@gmail.com>
5 ;; This file is part of GNU Guix.
7 ;; GNU Guix is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Guix is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;; This file provides functions to evaluate guile code using Geiser.
26 (require 'geiser-mode
)
29 (defun guix-geiser-repl ()
30 "Return the current Geiser REPL."
32 (geiser-repl--repl/impl
'guile
)
33 (error "Geiser REPL not found")))
35 (defun guix-geiser-eval (str &optional repl
)
36 "Evaluate STR with guile expression using Geiser REPL.
37 If REPL is nil, use the current Geiser REPL.
38 Return a list of strings with result values of evaluation."
39 (with-current-buffer (or repl
(guix-geiser-repl))
40 (let ((res (geiser-eval--send/wait
`(:eval
(:scm
,str
)))))
41 (if (geiser-eval--retort-error res
)
42 (error "Error in evaluating guile expression: %s"
43 (geiser-eval--retort-output res
))
44 (cdr (assq 'result res
))))))
46 (defun guix-geiser-eval-read (str &optional repl
)
47 "Evaluate STR with guile expression using Geiser REPL.
48 Return elisp expression of the first result value of evaluation."
49 ;; The goal is to convert a string with scheme expression into elisp
51 (let ((result (car (guix-geiser-eval str repl
))))
53 ((or (string= result
"#f")
54 (string= result
"#<unspecified>"))
56 ((string= result
"#t")
59 (read (replace-regexp-in-string
61 (replace-regexp-in-string
67 (defun guix-repl-send (cmd &optional save-history
)
68 "Send CMD input string to the current REPL buffer.
69 This is the same as `geiser-repl--send', but with SAVE-HISTORY
70 argument. If SAVE-HISTORY is non-nil, save CMD in the REPL
72 (when (and cmd
(eq major-mode
'geiser-repl-mode
))
73 (geiser-repl--prepare-send)
74 (goto-char (point-max))
77 (let ((comint-input-filter (if save-history
80 (comint-send-input nil t
))))
82 (defun guix-geiser-eval-in-repl (str &optional repl no-history no-display
)
83 "Switch to Geiser REPL and evaluate STR with guile expression there.
84 If NO-HISTORY is non-nil, do not save STR in the REPL history.
85 If NO-DISPLAY is non-nil, do not switch to the REPL buffer."
86 (let ((repl (or repl
(guix-geiser-repl))))
87 (with-current-buffer repl
88 ;; XXX Since Geiser 0.8, `geiser-repl--send' has SAVE-HISTORY
89 ;; argument, so use this function eventually and remove
91 (guix-repl-send str
(not no-history
)))
93 (geiser-repl--switch-to-buffer repl
))))
95 (defun guix-geiser-eval-in-repl-synchronously (str &optional repl
96 no-history no-display
)
97 "Evaluate STR in Geiser REPL synchronously, i.e. wait until the
98 REPL operation will be finished.
99 See `guix-geiser-eval-in-repl' for the meaning of arguments."
100 (let* ((repl (if repl
(get-buffer repl
) (guix-geiser-repl)))
102 (filter (lambda (output)
104 (and (get-buffer-process repl
)
105 (not (guix-guile-prompt? output
))))))
106 (comint-output-filter-functions
107 (cons filter comint-output-filter-functions
)))
108 (guix-geiser-eval-in-repl str repl no-history no-display
)
112 (defun guix-geiser-call (proc &rest args
)
113 "Call (PROC ARGS ...) synchronously using the current Geiser REPL.
114 PROC and ARGS should be strings."
116 (apply #'guix-guile-make-call-expression proc args
)))
118 (defun guix-geiser-call-in-repl (proc &rest args
)
119 "Call (PROC ARGS ...) in the current Geiser REPL.
120 PROC and ARGS should be strings."
121 (guix-geiser-eval-in-repl
122 (apply #'guix-guile-make-call-expression proc args
)))
124 (provide 'guix-geiser
)
126 ;;; guix-geiser.el ends here