guix-repl.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. ;;; guix-repl.el --- Making and using Guix REPL
  2. ;; Copyright © 2014–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 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://github.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. "List of directories prepended to Guile's `%load-path' when
  54. Guix REPL is started.
  55. Most likely you don't need to set this variable, but if you
  56. really do, note that these directories take precedence over any
  57. other added directory (including Guile modules of Emacs-Guix and
  58. Guix itself).
  59. Directories are used as is, without expanding, so make sure they
  60. do not contain things like \"~\" or \"..\" (use
  61. `expand-file-name').
  62. These directories are also prepended to `%load-compiled-path'
  63. unless `guix-load-compiled-path' is specified.")
  64. (defvar guix-load-compiled-path nil
  65. "List of directories prepended to Guile's `%load-compiled-path'
  66. when Guix REPL is started.
  67. See `guix-load-path' for details.")
  68. ;;; REPL
  69. (defgroup guix-repl nil
  70. "Settings for Guix REPLs."
  71. :prefix "guix-repl-"
  72. :group 'guix)
  73. (defcustom guix-repl-startup-time geiser-repl-startup-time
  74. "Time, in milliseconds, to wait for Guix REPL to startup.
  75. Same as `geiser-repl-startup-time' but is used for Guix REPL.
  76. If you have a slow system, try to increase this time."
  77. :type 'integer
  78. :group 'guix-repl)
  79. (defcustom guix-repl-buffer-name "*Guix REPL*"
  80. "Default name of a Geiser REPL buffer used for Guix."
  81. :type 'string
  82. :group 'guix-repl)
  83. (defcustom guix-repl-after-start-hook nil
  84. "Hook called after Guix REPL is started."
  85. :type 'hook
  86. :group 'guix-repl)
  87. (define-obsolete-variable-alias 'guix-use-guile-server
  88. 'guix-repl-use-server "0.2")
  89. (defcustom guix-repl-use-server t
  90. "If non-nil, start guile with '--listen' argument.
  91. This allows to receive information about packages using an
  92. additional (so called 'internal') REPL while some packages are
  93. being installed/removed in the main Guix REPL."
  94. :type 'boolean
  95. :group 'guix-repl)
  96. (defcustom guix-repl-use-latest t
  97. "If non-nil, use \"~/.config/guix/latest\" directory.
  98. It contains the latest Guix code populated after running \"guix pull\"."
  99. :type 'boolean
  100. :group 'guix-repl)
  101. (defcustom guix-repl-socket-file-name-function
  102. #'guix-repl-socket-file-name
  103. "Function used to define a socket file name used by Guix REPL.
  104. The function is called without arguments."
  105. :type '(choice (function-item guix-repl-socket-file-name)
  106. (function :tag "Other function"))
  107. :group 'guix-repl)
  108. (defcustom guix-emacs-activate-after-operation t
  109. "Activate Emacs packages after installing.
  110. If nil, do not load autoloads of the Emacs packages after
  111. they are successfully installed."
  112. :type 'boolean
  113. :group 'guix-repl)
  114. (defvar guix-repl-default-directory
  115. ;; If the current buffer is "tramp"-ed, the REPL may be started
  116. ;; with the root rights! So make sure `default-directory' is
  117. ;; "safe". See <https://notabug.org/alezost/emacs-guix/issues/1>
  118. ;; for details.
  119. (or (getenv "HOME")
  120. user-emacs-directory)
  121. "Guix REPL is started with this directory as `default-directory'.")
  122. (defvar guix-repl-current-socket nil
  123. "Name of a socket file used by the current Guix REPL.")
  124. (defvar guix-repl-buffer nil
  125. "Main Geiser REPL buffer used for communicating with Guix.
  126. This REPL is used for processing package actions and for
  127. receiving information if `guix-repl-use-server' is nil.")
  128. (defvar guix-internal-repl-buffer nil
  129. "Additional Geiser REPL buffer used for communicating with Guix.
  130. This REPL is used for receiving information only if
  131. `guix-repl-use-server' is non-nil.")
  132. (defvar guix-internal-repl-buffer-name "*Guix Internal REPL*"
  133. "Default name of an internal Guix REPL buffer.")
  134. (defvar guix-repl-before-operation-hook nil
  135. "Hook run before executing an operation in Guix REPL.")
  136. (defvar guix-repl-after-operation-hook
  137. '(guix-update-buffers-after-operation
  138. guix-repl-autoload-emacs-packages-maybe
  139. guix-repl-operation-success-message)
  140. "Hook run after executing successful operation in Guix REPL.")
  141. (defvar guix-repl-operation-p nil
  142. "Non-nil, if current operation is performed by `guix-eval-in-repl'.
  143. This internal variable is used to distinguish Guix operations
  144. from operations performed in Guix REPL by a user.")
  145. (defvar guix-repl-operation-type nil
  146. "Type of the current operation performed by `guix-eval-in-repl'.
  147. This internal variable is used to define what actions should be
  148. executed after the current operation succeeds.
  149. See `guix-eval-in-repl' for details.")
  150. (declare-function guix-emacs-autoload-packages "guix-emacs" t)
  151. (defun guix-repl-autoload-emacs-packages-maybe ()
  152. "Load autoloads for Emacs packages if needed.
  153. See `guix-emacs-activate-after-operation' for details."
  154. (and guix-emacs-activate-after-operation
  155. (require 'guix-emacs nil t)
  156. ;; FIXME Since a user can work with a non-current profile (using
  157. ;; C-u before `guix-search-by-name' and other commands), emacs
  158. ;; packages can be installed to another profile, and the
  159. ;; following code will not work (i.e., the autoloads for this
  160. ;; profile will not be loaded).
  161. (guix-emacs-autoload-packages guix-current-profile)))
  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" it) lp)
  173. (--mapcat (list "-C" 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. (let ((latest-dir (guix-latest-directory)))
  179. (list "-L" latest-dir
  180. "-C" latest-dir)))
  181. ,@(and guix-config-guix-scheme-directory
  182. (list "-L" guix-config-guix-scheme-directory
  183. "-C" (or guix-config-guix-scheme-compiled-directory
  184. guix-config-guix-scheme-directory)))
  185. ,@(and guix-repl-use-server
  186. (list (concat "--listen=" guix-repl-current-socket)))))
  187. (defun guix-repl-guile-program (&optional internal)
  188. "Return a value suitable for `geiser-guile-binary' to start Guix REPL.
  189. If INTERNAL is non-nil, return the value for the internal Guix REPL."
  190. (if internal
  191. guix-guile-program
  192. (append (guix-list-maybe guix-guile-program)
  193. (guix-repl-guile-args))))
  194. (defun guix-repl-socket-file-name ()
  195. "Return a fresh name of a socket file used by Guix REPL."
  196. (guix-temporary-file-name "repl-socket"))
  197. (defun guix-repl-delete-socket-maybe ()
  198. "Delete `guix-repl-current-socket' file if it exists."
  199. (and guix-repl-current-socket
  200. (file-exists-p guix-repl-current-socket)
  201. (delete-file guix-repl-current-socket)))
  202. (defun guix-start-process-maybe (&optional start-msg end-msg)
  203. "Start Geiser REPL configured for Guix if needed.
  204. START-MSG and END-MSG are strings displayed in the minibuffer in
  205. the beginning and in the end of the starting process. If nil,
  206. display default messages."
  207. (guix-start-repl-maybe nil
  208. (or start-msg "Starting Guix REPL ...")
  209. end-msg)
  210. (if guix-repl-use-server
  211. (guix-start-repl-maybe 'internal)
  212. (setq guix-internal-repl-buffer guix-repl-buffer)))
  213. (defun guix-start-repl-maybe (&optional internal start-msg end-msg)
  214. "Start Guix REPL if needed.
  215. If INTERNAL is non-nil, start an internal REPL.
  216. START-MSG and END-MSG are strings displayed in the minibuffer in
  217. the beginning and in the end of the process. If nil, do not
  218. display messages."
  219. (let* ((repl-var (guix-get-repl-buffer-variable internal))
  220. (repl (symbol-value repl-var))
  221. (default-directory guix-repl-default-directory))
  222. (unless (and (buffer-live-p repl)
  223. (get-buffer-process repl))
  224. (and start-msg (message start-msg))
  225. (setq guix-repl-operation-p nil)
  226. (unless internal
  227. ;; Guile leaves socket file after exit, so remove it if it
  228. ;; exists (after the REPL restart).
  229. (guix-repl-delete-socket-maybe)
  230. (setq guix-repl-current-socket
  231. (and guix-repl-use-server
  232. (or guix-repl-current-socket
  233. (funcall guix-repl-socket-file-name-function)))))
  234. (let ((geiser-guile-binary (guix-repl-guile-program internal))
  235. (repl (get-buffer-create
  236. (guix-get-repl-buffer-name internal))))
  237. (guix-start-repl repl (and internal guix-repl-current-socket))
  238. (set repl-var repl)
  239. ;; Wait until switching to (emacs-guix) module finishes.
  240. (guix-geiser-eval-in-repl-synchronously
  241. ",m (emacs-guix)" repl t t)
  242. (and end-msg (message end-msg))
  243. (unless internal
  244. (run-hooks 'guix-repl-after-start-hook))))))
  245. (defun guix-start-repl (buffer &optional address)
  246. "Start Guix REPL in BUFFER.
  247. If ADDRESS is non-nil, connect to a remote guile process using
  248. this address (it should be defined by
  249. `geiser-repl--read-address')."
  250. ;; A mix of the code from `geiser-repl--start-repl' and
  251. ;; `geiser-repl--to-repl-buffer'.
  252. (let ((impl 'guile)
  253. (geiser-repl-startup-time guix-repl-startup-time))
  254. (with-current-buffer buffer
  255. (geiser-repl-mode)
  256. (geiser-impl--set-buffer-implementation impl)
  257. (geiser-repl--autodoc-mode -1)
  258. (goto-char (point-max))
  259. (let ((prompt (geiser-con--combined-prompt
  260. geiser-guile--prompt-regexp
  261. geiser-guile--debugger-prompt-regexp)))
  262. (geiser-repl--save-remote-data address)
  263. (geiser-repl--start-scheme impl address prompt)
  264. (geiser-repl--quit-setup)
  265. (geiser-repl--history-setup)
  266. (setq-local geiser-repl--repls (list buffer))
  267. (geiser-repl--set-this-buffer-repl buffer)
  268. (setq geiser-repl--connection
  269. (geiser-con--make-connection
  270. (get-buffer-process (current-buffer))
  271. geiser-guile--prompt-regexp
  272. geiser-guile--debugger-prompt-regexp))
  273. (geiser-repl--startup impl address)
  274. (geiser-repl--autodoc-mode 1)
  275. (geiser-company--setup geiser-repl-company-p)
  276. (add-hook 'comint-output-filter-functions
  277. 'guix-repl-output-filter
  278. nil t)
  279. (set-process-query-on-exit-flag
  280. (get-buffer-process (current-buffer))
  281. geiser-repl-query-on-kill-p)))))
  282. (defun guix-repl-output-filter (str)
  283. "Filter function suitable for `comint-output-filter-functions'.
  284. This is a replacement for `geiser-repl--output-filter'."
  285. (cond
  286. ((string-match-p geiser-guile--prompt-regexp str)
  287. (geiser-autodoc--disinhibit-autodoc)
  288. (when guix-repl-operation-p
  289. (setq guix-repl-operation-p nil)
  290. (run-hooks 'guix-repl-after-operation-hook)
  291. ;; Run hooks specific to the current operation type.
  292. (when guix-repl-operation-type
  293. (let ((type-hook (intern
  294. (concat "guix-after-"
  295. (symbol-name guix-repl-operation-type)
  296. "-hook"))))
  297. (setq guix-repl-operation-type nil)
  298. (and (boundp type-hook)
  299. (run-hooks type-hook))))))
  300. ((string-match geiser-guile--debugger-prompt-regexp str)
  301. (setq guix-repl-operation-p nil)
  302. (geiser-con--connection-set-debugging geiser-repl--connection
  303. (match-beginning 0))
  304. (geiser-autodoc--disinhibit-autodoc))))
  305. (defun guix-repl-exit (&optional internal no-wait)
  306. "Exit the current Guix REPL.
  307. If INTERNAL is non-nil, exit the internal REPL.
  308. If NO-WAIT is non-nil, do not wait for the REPL process to exit:
  309. send a kill signal to it and return immediately."
  310. (let ((repl (symbol-value (guix-get-repl-buffer-variable internal))))
  311. (when (get-buffer-process repl)
  312. (with-current-buffer repl
  313. (geiser-con--connection-deactivate geiser-repl--connection t)
  314. (comint-kill-subjob)
  315. (unless no-wait
  316. (while (get-buffer-process repl)
  317. (sleep-for 0.1)))))))
  318. (defun guix-get-repl-buffer (&optional internal)
  319. "Return Guix REPL buffer; start REPL if needed.
  320. If INTERNAL is non-nil, return an additional internal REPL."
  321. (guix-start-process-maybe)
  322. (let ((repl (symbol-value (guix-get-repl-buffer-variable internal))))
  323. ;; If a new Geiser REPL is started, `geiser-repl--repl' variable may
  324. ;; be set to the new value in a Guix REPL, so set it back to a
  325. ;; proper value here.
  326. (with-current-buffer repl
  327. (geiser-repl--set-this-buffer-repl repl))
  328. repl))
  329. (defun guix-get-repl-buffer-variable (&optional internal)
  330. "Return the name of a variable with a REPL buffer."
  331. (if internal
  332. 'guix-internal-repl-buffer
  333. 'guix-repl-buffer))
  334. (defun guix-get-repl-buffer-name (&optional internal)
  335. "Return the name of a REPL buffer."
  336. (if internal
  337. guix-internal-repl-buffer-name
  338. guix-repl-buffer-name))
  339. (defun guix-switch-to-repl (&optional internal)
  340. "Switch to Guix REPL.
  341. If INTERNAL is non-nil (interactively with prefix), switch to the
  342. additional internal REPL if it exists."
  343. (interactive "P")
  344. (geiser-repl--switch-to-buffer (guix-get-repl-buffer internal)))
  345. ;;; Guix directory
  346. (defvar guix-directory nil
  347. "Default directory with Guix source.
  348. If it is not set by a user, it is set after starting Guix REPL.
  349. This directory is used to find packages and licenses by such
  350. commands as `guix-edit' or `guix-find-license-definition'.")
  351. (defun guix-read-directory ()
  352. "Return `guix-directory' or prompt for it.
  353. This function is intended for using in `interactive' forms."
  354. (if current-prefix-arg
  355. (read-directory-name "Directory with Guix modules: "
  356. guix-directory)
  357. guix-directory))
  358. (defun guix-latest-directory ()
  359. "Return 'guix pull'-ed directory."
  360. (let* ((config-dir (or (getenv "XDG_CONFIG_HOME")
  361. (expand-file-name "~/.config")))
  362. (latest-dir (expand-file-name "guix/latest" config-dir)))
  363. (if (file-exists-p latest-dir)
  364. (or guix-directory
  365. (setq guix-directory latest-dir))
  366. (message "Directory '%s' does not exist.
  367. Consider running \"guix pull\"." latest-dir))
  368. latest-dir))
  369. ;;; Operation buffers
  370. (define-obsolete-variable-alias 'guix-ui-update-after-operation
  371. 'guix-update-buffers-after-operation "0.2")
  372. (defcustom guix-update-buffers-after-operation 'current
  373. "Define what kind of data to update after executing an operation.
  374. After successful executing of some operation in the Guix
  375. REPL (for example after installing a package), the data in Guix
  376. buffers will or will not be automatically updated depending on a
  377. value of this variable.
  378. If nil, update nothing (do not revert any buffer).
  379. If `current', update the buffer from which an operation was performed.
  380. If `all', update all Guix buffers (not recommended)."
  381. :type '(choice (const :tag "Do nothing" nil)
  382. (const :tag "Update operation buffer" current)
  383. (const :tag "Update all Guix buffers" all))
  384. :group 'guix-repl)
  385. (defvar guix-operation-buffer nil
  386. "Buffer from which the latest Guix operation was performed.")
  387. (defun guix-operation-buffer? (&optional buffer modes)
  388. "Return non-nil if BUFFER mode is derived from any of the MODES.
  389. If BUFFER is nil, check current buffer.
  390. If MODES is nil, use modes for Guix package management."
  391. (with-current-buffer (or buffer (current-buffer))
  392. (apply #'derived-mode-p
  393. (or modes '(guix-package-list-mode
  394. guix-package-info-mode
  395. guix-output-list-mode
  396. guix-profile-list-mode
  397. guix-generation-list-mode
  398. guix-generation-info-mode)))))
  399. (defun guix-operation-buffers (&optional modes)
  400. "Return a list of all buffers with major modes derived from MODES.
  401. If MODES is nil, return list of all Guix 'list' and 'info' buffers."
  402. (--filter (guix-operation-buffer? it modes)
  403. (buffer-list)))
  404. (defun guix-update-buffers-after-operation ()
  405. "Update buffers after Guix operation if needed.
  406. See `guix-update-after-operation' for details."
  407. (let ((to-update
  408. (and guix-operation-buffer
  409. (cl-case guix-update-buffers-after-operation
  410. (current (and (buffer-live-p guix-operation-buffer)
  411. (guix-operation-buffer?
  412. guix-operation-buffer)
  413. (list guix-operation-buffer)))
  414. (all (guix-operation-buffers))))))
  415. (setq guix-operation-buffer nil)
  416. (dolist (buffer to-update)
  417. (with-current-buffer buffer
  418. (revert-buffer nil t)))))
  419. ;;; Evaluating expressions
  420. (defun guix-eval (str)
  421. "Evaluate STR with guile expression using Guix REPL.
  422. See `guix-geiser-eval' for details."
  423. (guix-geiser-eval str (guix-get-repl-buffer 'internal)))
  424. (defun guix-eval-read (str)
  425. "Evaluate STR with guile expression using Guix REPL.
  426. See `guix-geiser-eval-read' for details."
  427. (guix-geiser-eval-read str (guix-get-repl-buffer 'internal)))
  428. (defun guix-eval-in-repl (str &optional operation-buffer operation-type)
  429. "Switch to Guix REPL and evaluate STR with guile expression there.
  430. If OPERATION-BUFFER is non-nil, it should be a buffer from which
  431. the current operation was performed.
  432. If OPERATION-TYPE is non-nil, it should be a symbol. After
  433. successful executing of the current operation,
  434. `guix-after-OPERATION-TYPE-hook' is called."
  435. (run-hooks 'guix-repl-before-operation-hook)
  436. (setq guix-repl-operation-p t
  437. guix-repl-operation-type operation-type
  438. guix-operation-buffer operation-buffer)
  439. (guix-geiser-eval-in-repl str (guix-get-repl-buffer)))
  440. (provide 'guix-repl)
  441. ;;; guix-repl.el ends here