guix-repl.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. ;;; guix-repl.el --- Making and using Guix REPL
  2. ;; Copyright © 2014–2019 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 the code for interacting with Guile using Guix REPL
  18. ;; (Geiser REPL with some guix-specific additions).
  19. ;; By default (if `guix-repl-use-server' is non-nil) 2 Guix REPLs are
  20. ;; started. The main one (with "guile --listen" process) is used for
  21. ;; "interacting" with a user - for showing a progress of
  22. ;; installing/deleting Guix packages. The second (internal) REPL is
  23. ;; used for synchronous evaluating, e.g. when information about
  24. ;; packages/generations should be received for a list/info buffer.
  25. ;;
  26. ;; This "2 REPLs concept" makes it possible to have a running process of
  27. ;; installing/deleting packages and to continue to search/list/get info
  28. ;; about other packages at the same time. If you prefer to use a single
  29. ;; Guix REPL, do not try to receive any information while there is a
  30. ;; running code in the REPL (see
  31. ;; <https://gitlab.com/jaor/geiser/issues/28>).
  32. ;;
  33. ;; Guix REPLs (unlike the usual Geiser REPLs) are not added to
  34. ;; `geiser-repl--repls' variable, and thus cannot be used for evaluating
  35. ;; while editing .scm files. The only purpose of Guix REPLs is to be an
  36. ;; intermediate between "Guix/Guile level" and "Emacs interface level".
  37. ;; That being said, you can still wish to use a Guix REPL while hacking
  38. ;; Scheme files of Emacs-Guix. It is possible: you can just use
  39. ;; `geiser-connect-local' command with `guix-repl-current-socket' to
  40. ;; have a usual Geiser REPL with all stuff defined by Emacs-Guix
  41. ;; package (it is placed in (emacs-guix) module).
  42. ;;; Code:
  43. (require 'dash)
  44. (require 'geiser-mode)
  45. (require 'geiser-guile)
  46. (require 'guix nil t)
  47. (require 'guix-geiser)
  48. (require 'guix-config)
  49. (require 'guix-external)
  50. (require 'guix-profiles)
  51. (require 'guix-utils)
  52. (defvar guix-load-path nil
  53. "Directory or a list of directories prepended to Guile's
  54. `%load-path' when Guix REPL is started.
  55. These directories take precedence over any other
  56. directory (including Guile modules of Emacs-Guix and Guix
  57. itself). So this variable may be useful if you prefer to use
  58. guix from a git checkout:
  59. (setq guix-load-path \"/path/to/guix-from-git\")
  60. These directories are also prepended to `%load-compiled-path'
  61. unless `guix-load-compiled-path' is specified.")
  62. (defvar guix-load-compiled-path nil
  63. "List of directories prepended to Guile's `%load-compiled-path'
  64. when Guix REPL is started.
  65. See `guix-load-path' for details.")
  66. ;;; REPL
  67. (defgroup guix-repl nil
  68. "Settings for Guix REPLs."
  69. :prefix "guix-repl-"
  70. :group 'guix)
  71. (defcustom guix-repl-startup-time geiser-repl-startup-time
  72. "Time, in milliseconds, to wait for Guix REPL to startup.
  73. Same as `geiser-repl-startup-time' but is used for Guix REPL.
  74. If you have a slow system, try to increase this time."
  75. :type 'integer
  76. :group 'guix-repl)
  77. (defcustom guix-repl-buffer-name "*Guix REPL*"
  78. "Default name of a Geiser REPL buffer used for Guix."
  79. :type 'string
  80. :group 'guix-repl)
  81. (defcustom guix-repl-after-start-hook
  82. '(guix-repl-configure-guile-side)
  83. "Hook called after Guix REPL is started."
  84. :type 'hook
  85. :group 'guix-repl)
  86. (define-obsolete-variable-alias 'guix-use-guile-server
  87. 'guix-repl-use-server "0.2")
  88. (defcustom guix-repl-use-server t
  89. "If non-nil, start guile with '--listen' argument.
  90. This allows to receive information about packages using an
  91. additional (so called 'internal') REPL while some packages are
  92. being installed/removed in the main Guix REPL."
  93. :type 'boolean
  94. :group 'guix-repl)
  95. (defcustom guix-repl-use-latest t
  96. "If non-nil, use \"~/.config/guix/current\" directory.
  97. It contains the latest Guix code populated after running \"guix pull\"."
  98. :type 'boolean
  99. :group 'guix-repl)
  100. (defcustom guix-repl-socket-file-name-function
  101. #'guix-repl-socket-file-name
  102. "Function used to define a socket file name used by Guix REPL.
  103. The function is called without arguments."
  104. :type '(choice (function-item guix-repl-socket-file-name)
  105. (function :tag "Other function"))
  106. :group 'guix-repl)
  107. (defcustom guix-emacs-activate-after-operation t
  108. "Activate Emacs packages after installing.
  109. If nil, do not load autoloads of the Emacs packages after
  110. they are successfully installed."
  111. :type 'boolean
  112. :group 'guix-repl)
  113. (defvar guix-repl-default-directory
  114. ;; If the current buffer is "tramp"-ed, the REPL may be started
  115. ;; with the root rights! So make sure `default-directory' is
  116. ;; "safe". See <https://notabug.org/alezost/emacs-guix/issues/1>
  117. ;; for details.
  118. (or (getenv "HOME")
  119. user-emacs-directory)
  120. "Guix REPL is started with this directory as `default-directory'.")
  121. (defvar guix-repl-current-socket nil
  122. "Name of a socket file used by the current Guix REPL.")
  123. (defvar guix-repl-buffer nil
  124. "Main Geiser REPL buffer used for communicating with Guix.
  125. This REPL is used for processing package actions and for
  126. receiving information if `guix-repl-use-server' is nil.")
  127. (defvar guix-internal-repl-buffer nil
  128. "Additional Geiser REPL buffer used for communicating with Guix.
  129. This REPL is used for receiving information only if
  130. `guix-repl-use-server' is non-nil.")
  131. (defvar guix-internal-repl-buffer-name "*Guix Internal REPL*"
  132. "Default name of an internal Guix REPL buffer.")
  133. (defvar guix-repl-before-operation-hook nil
  134. "Hook run before executing an operation in Guix REPL.")
  135. (defvar guix-repl-after-operation-hook
  136. '(guix-update-buffers-after-operation
  137. guix-repl-operation-success-message)
  138. "Hook run after executing successful operation in Guix REPL.")
  139. (defvar guix-repl-operation-p nil
  140. "Non-nil, if current operation is performed by `guix-eval-in-repl'.
  141. This internal variable is used to distinguish Guix operations
  142. from operations performed in Guix REPL by a user.")
  143. (defvar guix-repl-operation-type nil
  144. "Type of the current operation performed by `guix-eval-in-repl'.
  145. This internal variable is used to define what actions should be
  146. executed after the current operation succeeds.
  147. See `guix-eval-in-repl' for details.")
  148. (defvar guix-repl-max-returned-list-size 10
  149. "Maximal length of a list that is passed from the Guile side to
  150. the Emacs side directly through Geiser.
  151. This is a performance variable: passing big chunks of data
  152. through Geiser may be slow, so to improve the speed, the Guile
  153. side can write data (like a list of packages to display) to a
  154. file, and then this file is read directly by Emacs.
  155. So if a list is lesser than the value of this variable, it is
  156. passed through Geiser. If it is bigger, it is written to a
  157. file (in `guix-temporary-directory').
  158. Set this variable to nil, if you want to pass a list of any size
  159. through Geiser (not recommended).
  160. After setting this variable, you need to kill
  161. `guix-repl-buffer-name' buffer to make the changes take effect.")
  162. (defun guix-repl-operation-success-message ()
  163. "Message telling about successful Guix operation."
  164. (message "Guix operation has been performed."))
  165. (defun guix-repl-guile-args ()
  166. "Return a list of Guile's arguments to start Guix REPL."
  167. `(,@(and guix-load-path
  168. (let* ((lp (guix-list-maybe guix-load-path))
  169. (lcp (if guix-load-compiled-path
  170. (guix-list-maybe guix-load-compiled-path)
  171. lp)))
  172. (append (--mapcat (list "-L" (guix-file-name it)) lp)
  173. (--mapcat (list "-C" (guix-file-name it)) lcp))))
  174. "-L" ,guix-scheme-directory
  175. ,@(and guix-config-scheme-compiled-directory
  176. (list "-C" guix-config-scheme-compiled-directory))
  177. ,@(and guix-repl-use-latest
  178. (if (file-exists-p guix-pulled-profile)
  179. (let ((scm-dir (guix-guile-site-directory
  180. guix-pulled-profile))
  181. (go-dir (guix-guile-site-directory
  182. guix-pulled-profile 'go)))
  183. (list "-L" scm-dir
  184. "-C" (or go-dir scm-dir)))
  185. ;; For backward compatibility.
  186. (--when-let (guix-latest-directory)
  187. (list "-L" it "-C" it))))
  188. ,@(and guix-config-guix-scheme-directory
  189. (list "-L" guix-config-guix-scheme-directory
  190. "-C" (or guix-config-guix-scheme-compiled-directory
  191. guix-config-guix-scheme-directory)))
  192. ,@(and guix-repl-use-server
  193. (list (concat "--listen=" guix-repl-current-socket)))))
  194. (defun guix-repl-guile-program (&optional internal)
  195. "Return a value suitable for `geiser-guile-binary' to start Guix REPL.
  196. If INTERNAL is non-nil, return the value for the internal Guix REPL."
  197. (if internal
  198. guix-guile-program
  199. (append (guix-list-maybe guix-guile-program)
  200. (guix-repl-guile-args))))
  201. (defun guix-repl-socket-file-name ()
  202. "Return a fresh name of a socket file used by Guix REPL."
  203. (guix-temporary-file-name "repl-socket"))
  204. (defun guix-repl-delete-socket-maybe ()
  205. "Delete `guix-repl-current-socket' file if it exists."
  206. (and guix-repl-current-socket
  207. (file-exists-p guix-repl-current-socket)
  208. (delete-file guix-repl-current-socket)))
  209. (defun guix-start-process-maybe (&optional start-msg end-msg)
  210. "Start Geiser REPL configured for Guix if needed.
  211. START-MSG and END-MSG are strings displayed in the minibuffer in
  212. the beginning and in the end of the starting process. If nil,
  213. display default messages."
  214. (guix-start-repl-maybe nil
  215. (or start-msg "Starting Guix REPL ...")
  216. end-msg)
  217. (if guix-repl-use-server
  218. (guix-start-repl-maybe 'internal)
  219. (setq guix-internal-repl-buffer guix-repl-buffer)))
  220. (defun guix-start-repl-maybe (&optional internal start-msg end-msg)
  221. "Start Guix REPL if needed.
  222. If INTERNAL is non-nil, start an internal REPL.
  223. START-MSG and END-MSG are strings displayed in the minibuffer in
  224. the beginning and in the end of the process. If nil, do not
  225. display messages."
  226. (let* ((repl-var (guix-get-repl-buffer-variable internal))
  227. (repl (symbol-value repl-var))
  228. (default-directory guix-repl-default-directory))
  229. (unless (and (buffer-live-p repl)
  230. (get-buffer-process repl))
  231. (and start-msg (message start-msg))
  232. (setq guix-repl-operation-p nil)
  233. (unless internal
  234. ;; Guile leaves socket file after exit, so remove it if it
  235. ;; exists (after the REPL restart).
  236. (guix-repl-delete-socket-maybe)
  237. (setq guix-repl-current-socket
  238. (and guix-repl-use-server
  239. (or guix-repl-current-socket
  240. (funcall guix-repl-socket-file-name-function)))))
  241. (let ((geiser-guile-binary (guix-repl-guile-program internal))
  242. (repl (get-buffer-create
  243. (guix-get-repl-buffer-name internal))))
  244. (guix-start-repl repl (and internal guix-repl-current-socket))
  245. (set repl-var repl)
  246. ;; Wait until switching to (emacs-guix) module finishes.
  247. (guix-geiser-eval-in-repl-synchronously
  248. ",m (emacs-guix)" repl t t)
  249. (and end-msg (message end-msg))
  250. (unless internal
  251. (run-hooks 'guix-repl-after-start-hook))))))
  252. (defun guix-start-repl (buffer &optional address)
  253. "Start Guix REPL in BUFFER.
  254. If ADDRESS is non-nil, connect to a remote guile process using
  255. this address (it should be defined by
  256. `geiser-repl--read-address')."
  257. ;; A mix of the code from `geiser-repl--start-repl' and
  258. ;; `geiser-repl--to-repl-buffer'.
  259. (let ((impl 'guile)
  260. (geiser-repl-startup-time guix-repl-startup-time))
  261. (with-current-buffer buffer
  262. (geiser-repl-mode)
  263. (geiser-impl--set-buffer-implementation impl)
  264. (geiser-repl--set-this-buffer-project 'guix)
  265. (geiser-repl--autodoc-mode -1)
  266. (goto-char (point-max))
  267. (let ((prompt (geiser-con--combined-prompt
  268. geiser-guile--prompt-regexp
  269. geiser-guile--debugger-prompt-regexp)))
  270. (geiser-repl--save-remote-data address)
  271. (geiser-repl--start-scheme impl address prompt)
  272. (geiser-repl--quit-setup)
  273. (geiser-repl--history-setup)
  274. (setq-local geiser-repl--repls (list buffer))
  275. (geiser-repl--set-this-buffer-repl buffer)
  276. (setq geiser-repl--connection
  277. (geiser-con--make-connection
  278. (get-buffer-process (current-buffer))
  279. geiser-guile--prompt-regexp
  280. geiser-guile--debugger-prompt-regexp))
  281. (geiser-repl--startup impl address)
  282. (geiser-repl--autodoc-mode 1)
  283. (geiser-company--setup geiser-repl-company-p)
  284. (add-hook 'comint-output-filter-functions
  285. 'guix-repl-output-filter
  286. nil t)
  287. (set-process-query-on-exit-flag
  288. (get-buffer-process (current-buffer))
  289. geiser-repl-query-on-kill-p)))))
  290. (defun guix-repl-output-filter (str)
  291. "Filter function suitable for `comint-output-filter-functions'.
  292. This is a replacement for `geiser-repl--output-filter'."
  293. (cond
  294. ((string-match-p geiser-guile--prompt-regexp str)
  295. (geiser-autodoc--disinhibit-autodoc)
  296. (when guix-repl-operation-p
  297. (setq guix-repl-operation-p nil)
  298. (run-hooks 'guix-repl-after-operation-hook)
  299. ;; Run hooks specific to the current operation type.
  300. (when guix-repl-operation-type
  301. (let ((type-hook (intern
  302. (concat "guix-after-"
  303. (symbol-name guix-repl-operation-type)
  304. "-hook"))))
  305. (setq guix-repl-operation-type nil)
  306. (and (boundp type-hook)
  307. (run-hooks type-hook))))))
  308. ((string-match geiser-guile--debugger-prompt-regexp str)
  309. (setq guix-repl-operation-p nil)
  310. (geiser-con--connection-set-debugging geiser-repl--connection
  311. (match-beginning 0))
  312. (geiser-autodoc--disinhibit-autodoc))))
  313. (defun guix-repl-configure-guile-side ()
  314. "Do some configurations of the Guix REPL.
  315. See `guix-repl-max-returned-list-size'."
  316. (let ((set-size (and guix-repl-max-returned-list-size
  317. (format "(set! %S %d)"
  318. '%max-returned-list-size
  319. guix-repl-max-returned-list-size)))
  320. (set-dir (format "(set! %S \"%s\")"
  321. '%temporary-directory
  322. (guix-temporary-directory))))
  323. (guix-eval (concat "(begin " set-size set-dir ")"))))
  324. (defun guix-repl-exit (&optional internal no-wait)
  325. "Exit the current Guix REPL.
  326. If INTERNAL is non-nil, exit the internal REPL.
  327. If NO-WAIT is non-nil, do not wait for the REPL process to exit:
  328. send a kill signal to it and return immediately."
  329. (let ((repl (symbol-value (guix-get-repl-buffer-variable internal))))
  330. (when (get-buffer-process repl)
  331. (with-current-buffer repl
  332. (geiser-con--connection-deactivate geiser-repl--connection t)
  333. (comint-kill-subjob)
  334. (unless no-wait
  335. (while (get-buffer-process repl)
  336. (sleep-for 0.1)))))))
  337. (defun guix-get-repl-buffer (&optional internal)
  338. "Return Guix REPL buffer; start REPL if needed.
  339. If INTERNAL is non-nil, return an additional internal REPL."
  340. (guix-start-process-maybe)
  341. (let ((repl (symbol-value (guix-get-repl-buffer-variable internal))))
  342. ;; If a new Geiser REPL is started, `geiser-repl--repl' variable may
  343. ;; be set to the new value in a Guix REPL, so set it back to a
  344. ;; proper value here.
  345. (with-current-buffer repl
  346. (geiser-repl--set-this-buffer-repl repl))
  347. repl))
  348. (defun guix-get-repl-buffer-variable (&optional internal)
  349. "Return the name of a variable with a REPL buffer."
  350. (if internal
  351. 'guix-internal-repl-buffer
  352. 'guix-repl-buffer))
  353. (defun guix-get-repl-buffer-name (&optional internal)
  354. "Return the name of a REPL buffer."
  355. (if internal
  356. guix-internal-repl-buffer-name
  357. guix-repl-buffer-name))
  358. (defun guix-switch-to-repl (&optional internal)
  359. "Switch to Guix REPL.
  360. If INTERNAL is non-nil (interactively with prefix), switch to the
  361. additional internal REPL if it exists."
  362. (interactive "P")
  363. (geiser-repl--switch-to-buffer (guix-get-repl-buffer internal)))
  364. ;;; Guix directory
  365. (defvar guix-directory nil
  366. "Default directory with Guix source.
  367. This directory is used to find packages and licenses by such
  368. commands as `guix-find-package-definition' or
  369. `guix-find-license-definition'.
  370. Most likely you don't need to set this variable because it is set
  371. automatically when needed. However, you can still set it if you
  372. really want; your value will not be overwritten.")
  373. (defun guix-directory ()
  374. "Set if needed and return `guix-directory'."
  375. (or guix-directory
  376. (let* ((guix.scm (guix-eval-read "(%search-load-path \"guix\")"))
  377. (dir (and guix.scm
  378. (file-name-directory guix.scm))))
  379. (setq guix-directory dir))))
  380. (defun guix-read-directory ()
  381. "Return `guix-directory' or prompt for it.
  382. This function is intended for using in `interactive' forms."
  383. (if current-prefix-arg
  384. (read-directory-name "Directory with Guix modules: "
  385. (guix-directory))
  386. (guix-directory)))
  387. ;; XXX Remove `guix-latest-directory' in future: it exists for backward
  388. ;; compatibility (in the past "guix pull" populated
  389. ;; "~/.config/guix/latest").
  390. (defun guix-latest-directory ()
  391. "Return 'guix pull'-ed directory or nil if it does not exist."
  392. (let* ((config-dir (or (getenv "XDG_CONFIG_HOME")
  393. (expand-file-name "~/.config")))
  394. (latest-dir (expand-file-name "guix/latest" config-dir)))
  395. (and (file-exists-p latest-dir)
  396. latest-dir)))
  397. ;;; Operation buffers
  398. (define-obsolete-variable-alias 'guix-ui-update-after-operation
  399. 'guix-update-buffers-after-operation "0.2")
  400. (defcustom guix-update-buffers-after-operation 'current
  401. "Define what kind of data to update after executing an operation.
  402. After successful executing of some operation in the Guix
  403. REPL (for example after installing a package), the data in Guix
  404. buffers will or will not be automatically updated depending on a
  405. value of this variable.
  406. If nil, update nothing (do not revert any buffer).
  407. If `current', update the buffer from which an operation was performed.
  408. If `all', update all Guix buffers (not recommended).
  409. See also `guix-ask-before-buffer-update'."
  410. :type '(choice (const :tag "Do nothing" nil)
  411. (const :tag "Update operation buffer" current)
  412. (const :tag "Update all Guix buffers" all))
  413. :group 'guix-repl)
  414. (defcustom guix-ask-before-buffer-update 1000
  415. "This variable defines if you are asked before buffer update or not.
  416. If nil, never ask; if t, always ask; if it is a number, ask only
  417. if the buffer displays this number of entries or more.
  418. See variable `guix-update-buffers-after-operation' for details on
  419. the buffer update."
  420. :type '(choice (const :tag "Always ask" t)
  421. (const :tag "Never ask" nil)
  422. (integer :tag "Ask if buffer shows this number of entries"))
  423. :group 'guix-repl)
  424. (defvar guix-operation-buffer nil
  425. "Buffer from which the latest Guix operation was performed.")
  426. (defun guix-operation-buffer? (&optional buffer modes)
  427. "Return non-nil if BUFFER mode is derived from any of the MODES.
  428. If BUFFER is nil, check current buffer.
  429. If MODES is nil, use modes for Guix package management."
  430. (with-current-buffer (or buffer (current-buffer))
  431. (apply #'derived-mode-p
  432. (or modes '(guix-package-list-mode
  433. guix-package-info-mode
  434. guix-output-list-mode
  435. guix-profile-list-mode
  436. guix-generation-list-mode
  437. guix-generation-info-mode)))))
  438. (defun guix-operation-buffers (&optional modes)
  439. "Return a list of all buffers with major modes derived from MODES.
  440. If MODES is nil, return list of all Guix 'list' and 'info' buffers."
  441. (--filter (guix-operation-buffer? it modes)
  442. (buffer-list)))
  443. (defun guix-update-buffers-after-operation ()
  444. "Update buffers after Guix operation if needed.
  445. See `guix-update-after-operation' for details."
  446. (let ((to-update
  447. (and guix-operation-buffer
  448. (cl-case guix-update-buffers-after-operation
  449. (current (and (buffer-live-p guix-operation-buffer)
  450. (list guix-operation-buffer)))
  451. (all (guix-operation-buffers))))))
  452. (setq guix-operation-buffer nil)
  453. (dolist (buffer to-update)
  454. (guix-update-buffer-maybe buffer))))
  455. (declare-function bui-current-entries "bui-core" t)
  456. (defun guix-update-buffer-maybe (buffer)
  457. "Update BUFFER if needed."
  458. (let ((ask (if (and (numberp guix-ask-before-buffer-update)
  459. (fboundp 'bui-current-entries))
  460. (with-current-buffer buffer
  461. (let ((entries (bui-current-entries)))
  462. (and entries
  463. (> (length entries)
  464. guix-ask-before-buffer-update))))
  465. guix-ask-before-buffer-update)))
  466. (when (or (not ask)
  467. (y-or-n-p (format "Update '%s' buffer? "
  468. (buffer-name buffer))))
  469. (with-current-buffer buffer
  470. (revert-buffer nil t)))))
  471. ;;; Evaluating expressions
  472. (defun guix-eval (str)
  473. "Evaluate STR with guile expression using Guix REPL.
  474. See `guix-geiser-eval' for details."
  475. (guix-geiser-eval str (guix-get-repl-buffer 'internal)))
  476. (defun guix-eval-read (str)
  477. "Evaluate STR with guile expression using Guix REPL.
  478. See `guix-geiser-eval-read' for details."
  479. (let ((result (guix-geiser-eval-read
  480. str (guix-get-repl-buffer 'internal))))
  481. (if (and (consp result)
  482. (eq (car result) 'in-file))
  483. (guix-guile-read-from-file (cdr result))
  484. result)))
  485. (defun guix-eval-in-repl (str &optional operation-buffer operation-type)
  486. "Switch to Guix REPL and evaluate STR with guile expression there.
  487. If OPERATION-BUFFER is non-nil, it should be a buffer from which
  488. the current operation was performed.
  489. If OPERATION-TYPE is non-nil, it should be a symbol. After
  490. successful executing of the current operation,
  491. `guix-after-OPERATION-TYPE-hook' is called."
  492. (run-hooks 'guix-repl-before-operation-hook)
  493. (setq guix-repl-operation-p t
  494. guix-repl-operation-type operation-type
  495. guix-operation-buffer operation-buffer)
  496. (guix-geiser-eval-in-repl str (guix-get-repl-buffer)))
  497. (provide 'guix-repl)
  498. ;;; guix-repl.el ends here