guix-repl.el 20 KB

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