guix-command.el 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. ;;; guix-command.el --- Popup interface for guix commands -*- lexical-binding: t -*-
  2. ;; Copyright © 2015–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 a magit-like popup interface for running guix
  18. ;; commands in Guix REPL. The entry point is "M-x guix". When it is
  19. ;; called the first time, "guix --help" output is parsed and
  20. ;; `guix-COMMAND-action' functions are generated for each available guix
  21. ;; COMMAND. Then a window with these commands is popped up. When a
  22. ;; particular COMMAND is called, "guix COMMAND --help" output is parsed,
  23. ;; and a user get a new popup window with available options for this
  24. ;; command and so on.
  25. ;; To avoid hard-coding all guix options, actions, etc., as much data is
  26. ;; taken from "guix ... --help" outputs as possible. But this data is
  27. ;; still incomplete: not all long options have short analogs, also
  28. ;; special readers should be used for some options (for example, to
  29. ;; complete package names while prompting for a package). So after
  30. ;; parsing --help output, the arguments are "improved". All arguments
  31. ;; (switches, options and actions) are `guix-command-argument'
  32. ;; structures.
  33. ;; Only "M-x guix" command is available after this file is loaded. The
  34. ;; rest commands/actions/popups are generated on the fly only when they
  35. ;; are needed (that's why there is a couple of `eval'-s in this file).
  36. ;; COMMANDS argument is used by many functions in this file. It means a
  37. ;; list of guix commands without "guix" itself, e.g.: ("build"),
  38. ;; ("import" "gnu"). The empty list stands for the plain "guix" without
  39. ;; subcommands.
  40. ;; All actions in popup windows are divided into 2 groups:
  41. ;;
  42. ;; - 'Popup' actions - used to pop up another window. For example, every
  43. ;; action in the 'guix' or 'guix import' window is a popup action. They
  44. ;; are defined by `guix-command-define-popup-action' macro.
  45. ;;
  46. ;; - 'Execute' actions - used to do something with the command line (to
  47. ;; run a command in Guix REPL or to copy it into kill-ring) constructed
  48. ;; with the current popup. They are defined by
  49. ;; `guix-command-define-execute-action' macro.
  50. ;;; Code:
  51. (require 'cl-lib)
  52. (require 'bui-utils)
  53. (require 'guix nil t)
  54. (require 'guix-popup)
  55. (require 'guix-utils)
  56. (require 'guix-help-vars)
  57. (require 'guix-read)
  58. (require 'guix-misc)
  59. (require 'guix-build-log)
  60. (require 'guix-guile)
  61. (require 'guix-external)
  62. (defgroup guix-commands nil
  63. "Settings for guix popup windows."
  64. :group 'guix)
  65. (defvar guix-command-complex-with-shared-arguments
  66. '("potluck" "system")
  67. "List of guix commands which have subcommands with shared options.
  68. I.e., 'guix foo --help' is the same as 'guix foo bar --help'.")
  69. (defun guix-command-action-name (&optional commands &rest name-parts)
  70. "Return name of action function for guix COMMANDS."
  71. (guix-command-symbol (append commands name-parts (list "action"))))
  72. ;;; Command arguments
  73. (cl-defstruct (guix-command-argument
  74. (:constructor guix-command-make-argument)
  75. (:copier guix-command-copy-argument))
  76. name char doc fun switch? option? action?)
  77. (cl-defun guix-command-modify-argument
  78. (argument &key
  79. (name nil name-bound?)
  80. (char nil char-bound?)
  81. (doc nil doc-bound?)
  82. (fun nil fun-bound?)
  83. (switch? nil switch?-bound?)
  84. (option? nil option?-bound?)
  85. (action? nil action?-bound?))
  86. "Return a modified version of ARGUMENT."
  87. (declare (indent 1))
  88. (let ((copy (guix-command-copy-argument argument)))
  89. (and name-bound? (setf (guix-command-argument-name copy) name))
  90. (and char-bound? (setf (guix-command-argument-char copy) char))
  91. (and doc-bound? (setf (guix-command-argument-doc copy) doc))
  92. (and fun-bound? (setf (guix-command-argument-fun copy) fun))
  93. (and switch?-bound? (setf (guix-command-argument-switch? copy) switch?))
  94. (and option?-bound? (setf (guix-command-argument-option? copy) option?))
  95. (and action?-bound? (setf (guix-command-argument-action? copy) action?))
  96. copy))
  97. (defun guix-command-modify-argument-from-alist (argument alist)
  98. "Return a modified version of ARGUMENT or nil if it wasn't modified.
  99. Each assoc from ALIST have a form (NAME . PLIST). NAME is an
  100. argument name. PLIST is a property list of argument parameters
  101. to be modified."
  102. (let* ((name (guix-command-argument-name argument))
  103. (plist (bui-assoc-value alist name)))
  104. (when plist
  105. (apply #'guix-command-modify-argument
  106. argument plist))))
  107. (defmacro guix-command-define-argument-improver (name alist)
  108. "Define NAME variable and function to modify an argument from ALIST."
  109. (declare (indent 1))
  110. `(progn
  111. (defvar ,name ,alist)
  112. (defun ,name (argument)
  113. (guix-command-modify-argument-from-alist argument ,name))))
  114. (guix-command-define-argument-improver
  115. guix-command-improve-action-argument
  116. '(("container" :char ?C)
  117. ("copy" :char ?y)
  118. ("graph" :char ?G)
  119. ("environment" :char ?E)
  120. ("pack" :char ?k)
  121. ("potluck" :char ?L)
  122. ("publish" :char ?u)
  123. ("pull" :char ?P)
  124. ("size" :char ?z)))
  125. (guix-command-define-argument-improver
  126. guix-command-improve-common-argument
  127. '(("--help" :switch? nil)
  128. ("--version" :switch? nil)))
  129. (guix-command-define-argument-improver
  130. guix-command-improve-target-argument
  131. '(("--target" :char ?T)))
  132. (guix-command-define-argument-improver
  133. guix-command-improve-system-type-argument
  134. '(("--system" :fun guix-read-system-type)))
  135. (guix-command-define-argument-improver
  136. guix-command-improve-load-path-argument
  137. '(("--load-path" :fun read-directory-name)))
  138. (guix-command-define-argument-improver
  139. guix-command-improve-search-paths-argument
  140. '(("--search-paths" :char ?P)))
  141. (guix-command-define-argument-improver
  142. guix-command-improve-substitute-urls-argument
  143. '(("--substitute-urls" :char ?U)))
  144. (guix-command-define-argument-improver
  145. guix-command-improve-hash-argument
  146. '(("--format" :fun guix-read-hash-format)))
  147. (guix-command-define-argument-improver
  148. guix-command-improve-key-policy-argument
  149. '(("--key-download" :fun guix-read-key-policy)))
  150. (defvar guix-command-improve-common-build-argument
  151. '(("--no-substitutes" :char ?s)
  152. ("--no-build-hook" :char ?h)
  153. ("--max-silent-time" :char ?X)
  154. ("--rounds" :char ?R :fun read-number)
  155. ("--no-grafts" :char ?G)
  156. ("--with-graft" :char ?g)
  157. ("--with-input" :char ?W)))
  158. (defun guix-command-improve-common-build-argument (argument)
  159. (guix-command-modify-argument-from-alist
  160. argument
  161. (append guix-command-improve-load-path-argument
  162. guix-command-improve-substitute-urls-argument
  163. guix-command-improve-common-build-argument)))
  164. (guix-command-define-argument-improver
  165. guix-command-improve-archive-argument
  166. '(("--extract" :fun read-directory-name)))
  167. (guix-command-define-argument-improver
  168. guix-command-improve-build-argument
  169. '(("--file" :fun guix-read-file-name)
  170. ("--root" :fun guix-read-file-name)
  171. ("--sources" :char ?S :fun guix-read-source-type :switch? nil)
  172. ("--with-source" :fun guix-read-file-name)))
  173. (guix-command-define-argument-improver
  174. guix-command-improve-copy-argument
  175. '(("--to" :char ?T)))
  176. (guix-command-define-argument-improver
  177. guix-command-improve-environment-argument
  178. '(("--ad-hoc"
  179. :name "--ad-hoc " :fun guix-read-package-names-string
  180. :switch? nil :option? t)
  181. ("--expose" :char ?E)
  182. ("--share" :char ?S)
  183. ("--load" :fun guix-read-file-name)))
  184. (guix-command-define-argument-improver
  185. guix-command-improve-gc-argument
  186. '(("--list-dead" :char ?D)
  187. ("--list-live" :char ?L)
  188. ("--referrers" :char ?f)
  189. ("--verify" :fun guix-read-verify-options-string)))
  190. (guix-command-define-argument-improver
  191. guix-command-improve-graph-argument
  192. '(("--list-backends" :char ?b)
  193. ("--list-types" :char ?t)
  194. ("--backend" :fun guix-read-graph-backend)
  195. ("--type" :fun guix-read-graph-node-type)))
  196. (guix-command-define-argument-improver
  197. guix-command-improve-import-argument
  198. '(("gem" :char ?G)
  199. ("crate" :char ?C)
  200. ("cran" :char ?r)))
  201. (guix-command-define-argument-improver
  202. guix-command-improve-import-elpa-argument
  203. '(("--archive" :fun guix-read-elpa-archive)))
  204. (guix-command-define-argument-improver
  205. guix-command-improve-lint-argument
  206. '(("--checkers" :fun guix-read-lint-checker-names-string)))
  207. (guix-command-define-argument-improver
  208. guix-command-improve-pack-argument
  209. '(("--compression" :fun guix-read-compressor-name)
  210. ("--format" :fun guix-read-pack-format-name)
  211. ;; "--symlink" is not completed as it should be "FILE-NAME=TARGET".
  212. ;; ("--symlink" :fun guix-read-file-name)
  213. ))
  214. (guix-command-define-argument-improver
  215. guix-command-improve-package-argument
  216. ;; Unlike all other options, --install/--remove do not have a form
  217. ;; '--install=foo,bar' but '--install foo bar' instead, so we need
  218. ;; some tweaks.
  219. '(("--install"
  220. :name "--install " :fun guix-read-package-names-string
  221. :switch? nil :option? t)
  222. ("--remove"
  223. :name "--remove " :fun guix-read-package-names-string
  224. :switch? nil :option? t)
  225. ("--install-from-file" :fun guix-read-file-name)
  226. ("--manifest" :fun guix-read-file-name)
  227. ("--profile" :fun guix-read-file-name)
  228. ;; Although it is documented that regexp is optional for --upgrade
  229. ;; and --do-not-upgrade, use them only as options (not as switches).
  230. ("--upgrade" :switch? nil)
  231. ("--do-not-upgrade" :char ?n :switch? nil)
  232. ("--roll-back" :char ?R)
  233. ("--show" :char ?h :fun guix-read-package-name)))
  234. (guix-command-define-argument-improver
  235. guix-command-improve-potluck-argument
  236. ;; TODO Add completions for "--license".
  237. '(("--scratch" :fun read-directory-name)
  238. ("--source" :char ?S :fun read-directory-name)
  239. ("--target" :fun read-directory-name)))
  240. (guix-command-define-argument-improver
  241. guix-command-improve-publish-argument
  242. '(("--public-key" :char ?k :fun guix-read-file-name)
  243. ("--private-key" :char ?K :fun guix-read-file-name)
  244. ("--user" :fun guix-read-user-name)))
  245. (guix-command-define-argument-improver
  246. guix-command-improve-refresh-argument
  247. '(("--select" :fun guix-read-refresh-subset)
  248. ("--type" :fun guix-read-refresh-updater-names-string)
  249. ("--key-server" :char ?S)))
  250. (guix-command-define-argument-improver
  251. guix-command-improve-size-argument
  252. '(("--map-file" :fun guix-read-file-name)))
  253. (guix-command-define-argument-improver
  254. guix-command-improve-system-argument
  255. '(("disk-image" :char ?D)
  256. ("vm-image" :char ?V)
  257. ("roll-back" :char ?R)
  258. ("switch-generation" :char ?S)
  259. ("--on-error" :char ?E)
  260. ("--no-grub" :char ?g)
  261. ("--full-boot" :char ?b)))
  262. (defvar guix-command-argument-improvers
  263. '((()
  264. guix-command-improve-action-argument)
  265. (("archive")
  266. guix-command-improve-common-build-argument
  267. guix-command-improve-target-argument
  268. guix-command-improve-system-type-argument
  269. guix-command-improve-archive-argument)
  270. (("build")
  271. guix-command-improve-common-build-argument
  272. guix-command-improve-target-argument
  273. guix-command-improve-system-type-argument
  274. guix-command-improve-build-argument)
  275. (("copy")
  276. guix-command-improve-common-build-argument
  277. guix-command-improve-copy-argument)
  278. (("download")
  279. guix-command-improve-hash-argument)
  280. (("hash")
  281. guix-command-improve-hash-argument)
  282. (("environment")
  283. guix-command-improve-common-build-argument
  284. guix-command-improve-search-paths-argument
  285. guix-command-improve-system-type-argument
  286. guix-command-improve-environment-argument)
  287. (("gc")
  288. guix-command-improve-gc-argument)
  289. (("graph")
  290. guix-command-improve-graph-argument)
  291. (("import")
  292. guix-command-improve-import-argument)
  293. (("import" "gnu")
  294. guix-command-improve-key-policy-argument)
  295. (("import" "elpa")
  296. guix-command-improve-import-elpa-argument)
  297. (("lint")
  298. guix-command-improve-lint-argument)
  299. (("pack")
  300. guix-command-improve-common-build-argument
  301. guix-command-improve-system-type-argument
  302. guix-command-improve-target-argument
  303. guix-command-improve-pack-argument)
  304. (("package")
  305. guix-command-improve-common-build-argument
  306. guix-command-improve-search-paths-argument
  307. guix-command-improve-package-argument)
  308. (("potluck")
  309. guix-command-improve-potluck-argument)
  310. (("publish")
  311. guix-command-improve-publish-argument)
  312. (("refresh")
  313. guix-command-improve-key-policy-argument
  314. guix-command-improve-refresh-argument)
  315. (("size")
  316. guix-command-improve-system-type-argument
  317. guix-command-improve-substitute-urls-argument
  318. guix-command-improve-size-argument)
  319. (("system")
  320. guix-command-improve-common-build-argument
  321. guix-command-improve-system-argument))
  322. "Alist of guix commands and argument improvers for them.")
  323. (defun guix-command-improve-argument (argument improvers)
  324. "Return ARGUMENT modified with IMPROVERS."
  325. (or (cl-some (lambda (improver)
  326. (funcall improver argument))
  327. improvers)
  328. argument))
  329. (defun guix-command-improve-arguments (arguments commands)
  330. "Return ARGUMENTS for 'guix COMMANDS ...' modified for popup interface."
  331. (let ((improvers (cons 'guix-command-improve-common-argument
  332. (bui-assoc-value guix-command-argument-improvers
  333. commands))))
  334. (mapcar (lambda (argument)
  335. (guix-command-improve-argument argument improvers))
  336. arguments)))
  337. (defun guix-command-parse-arguments (&optional commands)
  338. "Return a list of parsed 'guix COMMANDS ...' arguments."
  339. (with-temp-buffer
  340. (insert (guix-help-string commands))
  341. (let (args)
  342. (guix-while-search guix-help-parse-option-regexp
  343. (let* ((short (match-string-no-properties 1))
  344. (name (match-string-no-properties 2))
  345. (arg (match-string-no-properties 3))
  346. (doc (match-string-no-properties 4))
  347. (char (if short
  348. (elt short 1) ; short option letter
  349. (elt name 2))) ; first letter of the long option
  350. ;; If "--foo=bar" or "--foo[=bar]" then it is 'option'.
  351. (option? (not (string= "" arg)))
  352. ;; If "--foo" or "--foo[=bar]" then it is 'switch'.
  353. (switch? (or (string= "" arg)
  354. (eq ?\[ (elt arg 0)))))
  355. (push (guix-command-make-argument
  356. :name name
  357. :char char
  358. :doc doc
  359. :switch? switch?
  360. :option? option?)
  361. args)))
  362. (guix-while-search guix-help-parse-command-regexp
  363. (let* ((name (match-string-no-properties 1))
  364. (char (elt name 0)))
  365. (push (guix-command-make-argument
  366. :name name
  367. :char char
  368. :fun (guix-command-action-name commands name)
  369. :action? t)
  370. args)))
  371. args)))
  372. (defun guix-command-rest-argument (&optional commands)
  373. "Return '--' argument for COMMANDS."
  374. (cl-flet ((argument (&rest args)
  375. (apply #'guix-command-make-argument
  376. :name "-- " :char ?= :option? t args)))
  377. (let ((command (car commands)))
  378. (cond
  379. ((member command
  380. '("archive" "build" "challenge" "copy" "edit"
  381. "graph" "lint" "pack" "refresh"))
  382. (argument :doc "Packages" :fun 'guix-read-package-names-string))
  383. ((equal commands '("container" "exec"))
  384. (argument :doc "PID Command [Args...]"))
  385. ((string= command "download")
  386. (argument :doc "URL"))
  387. ((string= command "environment")
  388. (argument :doc "Command [Args...]" :fun 'read-shell-command))
  389. ((string= command "potluck")
  390. (argument :doc "[Args...]"))
  391. ((string= command "gc")
  392. (argument :doc "Paths" :fun 'guix-read-file-name))
  393. ((member command '("hash" "system"))
  394. (argument :doc "File" :fun 'guix-read-file-name))
  395. ((string= command "size")
  396. (argument :doc "Package" :fun 'guix-read-package-name))
  397. ((equal commands '("import" "nix"))
  398. (argument :doc "Nixpkgs Attribute"))
  399. ;; Other 'guix import' subcommands, but not 'import' itself.
  400. ((and (cdr commands)
  401. (string= command "import"))
  402. (argument :doc "Package name"))))))
  403. (defvar guix-command-additional-arguments
  404. `((("environment")
  405. ,(guix-command-make-argument
  406. :name "++packages " :char ?p :option? t
  407. :doc "build inputs of the specified packages"
  408. :fun 'guix-read-package-names-string)))
  409. "Alist of guix commands and additional arguments for them.
  410. These are 'fake' arguments that are not presented in 'guix' shell
  411. commands.")
  412. (defun guix-command-additional-arguments (&optional commands)
  413. "Return additional arguments for COMMANDS."
  414. (let ((rest-arg (guix-command-rest-argument commands)))
  415. (append (bui-assoc-value guix-command-additional-arguments
  416. commands)
  417. (and rest-arg (list rest-arg)))))
  418. ;; Ideally only `guix-command-arguments' function should exist with the
  419. ;; contents of `guix-command-all-arguments', but we need to make a
  420. ;; special case for `guix-command-complex-with-shared-arguments' commands.
  421. (defun guix-command-all-arguments (&optional commands)
  422. "Return list of all arguments for 'guix COMMANDS ...'."
  423. (let ((parsed (guix-command-parse-arguments commands)))
  424. (append (guix-command-improve-arguments parsed commands)
  425. (guix-command-additional-arguments commands))))
  426. (guix-memoized-defalias guix-command-all-arguments-memoize
  427. guix-command-all-arguments)
  428. (defun guix-command-arguments (&optional commands)
  429. "Return list of arguments for 'guix COMMANDS ...'."
  430. (let ((command (car commands)))
  431. (if (member command
  432. guix-command-complex-with-shared-arguments)
  433. ;; Take actions only for 'guix system', and switches+options for
  434. ;; 'guix system foo'.
  435. (funcall (if (null (cdr commands))
  436. #'cl-remove-if-not
  437. #'cl-remove-if)
  438. #'guix-command-argument-action?
  439. (guix-command-all-arguments-memoize (list command)))
  440. (guix-command-all-arguments commands))))
  441. (defun guix-command-switch->popup-switch (switch)
  442. "Return popup switch from command SWITCH argument."
  443. (list (guix-command-argument-char switch)
  444. (or (guix-command-argument-doc switch)
  445. "Unknown")
  446. (guix-command-argument-name switch)))
  447. (defun guix-command-option->popup-option (option)
  448. "Return popup option from command OPTION argument."
  449. (list (guix-command-argument-char option)
  450. (or (guix-command-argument-doc option)
  451. "Unknown")
  452. (let ((name (guix-command-argument-name option)))
  453. (if (string-match-p " \\'" name) ; ends with space
  454. name
  455. (concat name "=")))
  456. (or (guix-command-argument-fun option)
  457. 'read-from-minibuffer)))
  458. (defun guix-command-action->popup-action (action)
  459. "Return popup action from command ACTION argument."
  460. (list (guix-command-argument-char action)
  461. (or (guix-command-argument-doc action)
  462. (guix-command-argument-name action)
  463. "Unknown")
  464. (guix-command-argument-fun action)))
  465. (defun guix-command-sort-arguments (arguments)
  466. "Sort ARGUMENTS by name in alphabetical order."
  467. (sort arguments
  468. (lambda (a1 a2)
  469. (let ((name1 (guix-command-argument-name a1))
  470. (name2 (guix-command-argument-name a2)))
  471. (cond ((null name1) nil)
  472. ((null name2) t)
  473. (t (string< name1 name2)))))))
  474. (defun guix-command-switches (arguments)
  475. "Return switches from ARGUMENTS."
  476. (cl-remove-if-not #'guix-command-argument-switch? arguments))
  477. (defun guix-command-options (arguments)
  478. "Return options from ARGUMENTS."
  479. (cl-remove-if-not #'guix-command-argument-option? arguments))
  480. (defun guix-command-actions (arguments)
  481. "Return actions from ARGUMENTS."
  482. (cl-remove-if-not #'guix-command-argument-action? arguments))
  483. ;;; Post processing popup arguments
  484. (defvar guix-command-post-processors
  485. '(("environment"
  486. guix-command-post-process-environment-packages
  487. guix-command-post-process-environment-ad-hoc
  488. guix-command-post-process-rest-multiple-leave)
  489. ("hash"
  490. guix-command-post-process-rest-single)
  491. ("package"
  492. guix-command-post-process-package-args)
  493. ("system"
  494. guix-command-post-process-rest-single))
  495. "Alist of guix commands and functions for post-processing
  496. a list of arguments returned from popup interface.
  497. Each function is called on the returned arguments in turn.")
  498. (defvar guix-command-rest-arg-regexp
  499. (rx string-start "-- " (group (+ any)))
  500. "Regexp to match a string with the 'rest' arguments.")
  501. (defun guix-command-replace-args (args predicate modifier)
  502. "Replace arguments matching PREDICATE from ARGS.
  503. Call MODIFIER on each argument matching PREDICATE and append the
  504. returned list of strings to the end of ARGS. Remove the original
  505. arguments."
  506. (let* ((rest nil)
  507. (args (mapcar (lambda (arg)
  508. (if (funcall predicate arg)
  509. (progn
  510. (push (funcall modifier arg) rest)
  511. nil)
  512. arg))
  513. args)))
  514. (if rest
  515. (apply #'append (delq nil args) rest)
  516. args)))
  517. (cl-defun guix-command-post-process-matching-args (args regexp
  518. &key group split?)
  519. "Modify arguments from ARGS matching REGEXP by moving them to
  520. the end of ARGS list. If SPLIT? is non-nil, split matching
  521. arguments into multiple subarguments."
  522. (guix-command-replace-args
  523. args
  524. (lambda (arg)
  525. (string-match regexp arg))
  526. (lambda (arg)
  527. (let ((val (match-string (or group 0) arg))
  528. (fun (if split? #'split-string #'list)))
  529. (funcall fun val)))))
  530. (defun guix-command-post-process-rest-single (args)
  531. "Modify ARGS by moving '-- ARG' argument to the end of ARGS list."
  532. (guix-command-post-process-matching-args
  533. args guix-command-rest-arg-regexp
  534. :group 1))
  535. (defun guix-command-post-process-rest-multiple (args)
  536. "Modify ARGS by splitting '-- ARG ...' into multiple subarguments
  537. and moving them to the end of ARGS list.
  538. Remove '-- ' string."
  539. (guix-command-post-process-matching-args
  540. args guix-command-rest-arg-regexp
  541. :group 1
  542. :split? t))
  543. (defun guix-command-post-process-rest-multiple-leave (args)
  544. "Modify ARGS by splitting '-- ARG ...' into multiple subarguments
  545. and moving them to the end of ARGS list.
  546. Leave '--' string as a separate argument."
  547. (guix-command-post-process-matching-args
  548. args guix-command-rest-arg-regexp
  549. :split? t))
  550. (defun guix-command-post-process-package-args (args)
  551. "Adjust popup ARGS for 'guix package' command."
  552. (guix-command-post-process-matching-args
  553. args (rx string-start (or "--install " "--remove ") (+ any))
  554. :split? t))
  555. (defun guix-command-post-process-environment-packages (args)
  556. "Adjust popup ARGS for specified packages of 'guix environment'
  557. command."
  558. (guix-command-post-process-matching-args
  559. args (rx string-start "++packages " (group (+ any)))
  560. :group 1
  561. :split? t))
  562. (defun guix-command-post-process-environment-ad-hoc (args)
  563. "Adjust popup ARGS for '--ad-hoc' argument of 'guix environment'
  564. command."
  565. (guix-command-post-process-matching-args
  566. args (rx string-start "--ad-hoc " (+ any))
  567. :split? t))
  568. (defun guix-command-post-process-args (commands args)
  569. "Adjust popup ARGS for guix COMMANDS."
  570. (let* ((command (car commands))
  571. (processors
  572. (append (bui-assoc-value guix-command-post-processors commands)
  573. (bui-assoc-value guix-command-post-processors command))))
  574. (apply #'guix-modify
  575. args
  576. (or processors
  577. (list #'guix-command-post-process-rest-multiple)))))
  578. ;;; 'Execute' actions
  579. (defvar guix-command-default-execute-arguments
  580. (list
  581. (guix-command-make-argument
  582. :name "repl" :char ?r :doc "Run in Guix REPL")
  583. (guix-command-make-argument
  584. :name "shell" :char ?s :doc "Run in shell")
  585. (guix-command-make-argument
  586. :name "copy" :char ?c :doc "Copy command line"))
  587. "List of default 'execute' action arguments.")
  588. (defvar guix-command-additional-execute-arguments
  589. (let ((graph-arg (guix-command-make-argument
  590. :name "view" :char ?v :doc "View graph")))
  591. `((("build")
  592. ,(guix-command-make-argument
  593. :name "log" :char ?l :doc "View build log"))
  594. (("graph") ,graph-arg)
  595. (("size")
  596. ,(guix-command-make-argument
  597. :name "view" :char ?v :doc "View map"))
  598. (("system" "shepherd-graph") ,graph-arg)
  599. (("system" "extension-graph") ,graph-arg)))
  600. "Alist of guix commands and additional 'execute' action arguments.")
  601. (defun guix-command-execute-arguments (commands)
  602. "Return a list of 'execute' action arguments for COMMANDS."
  603. (mapcar (lambda (arg)
  604. (guix-command-modify-argument arg
  605. :action? t
  606. :fun (guix-command-action-name
  607. commands (guix-command-argument-name arg))))
  608. (append guix-command-default-execute-arguments
  609. (bui-assoc-value
  610. guix-command-additional-execute-arguments commands))))
  611. (defvar guix-command-special-executors
  612. '((("environment")
  613. ("repl" . guix-run-environment-command-in-repl))
  614. (("pull")
  615. ("repl" . guix-run-pull-command-in-repl))
  616. (("build")
  617. ("log" . guix-run-view-build-log))
  618. (("graph")
  619. ("view" . guix-run-view-graph))
  620. (("size")
  621. ("view" . guix-run-view-size-map))
  622. (("system" "shepherd-graph")
  623. ("view" . guix-run-view-graph))
  624. (("system" "extension-graph")
  625. ("view" . guix-run-view-graph)))
  626. "Alist of guix commands and alists of special executers for them.
  627. See also `guix-command-default-executors'.")
  628. (defvar guix-command-default-executors
  629. '(("repl" . guix-run-command-in-repl)
  630. ("shell" . guix-run-command-in-shell)
  631. ("copy" . guix-copy-command-as-kill))
  632. "Alist of default executers for action names.")
  633. (defun guix-command-executor (commands name)
  634. "Return function to run command line arguments for guix COMMANDS."
  635. (or (bui-assoc-value guix-command-special-executors commands name)
  636. (bui-assoc-value guix-command-default-executors name)))
  637. (defun guix-run-environment-command-in-repl (args)
  638. "Run 'guix ARGS ...' environment command in Guix REPL."
  639. ;; As 'guix environment' usually tries to run another process, it may
  640. ;; be fun but not wise to run this command in Geiser REPL.
  641. (when (or (member "--dry-run" args)
  642. (member "--search-paths" args)
  643. (when (y-or-n-p
  644. (format "'%s' command will spawn an external process.
  645. Do you really want to execute this command in Geiser REPL? "
  646. (guix-command-string args)))
  647. (message (substitute-command-keys
  648. "May \"\\[shell-mode]\" be with you!"))
  649. t))
  650. (guix-run-command-in-repl args)))
  651. (defun guix-run-pull-command-in-repl (args)
  652. "Run 'guix ARGS ...' pull command in Guix REPL.
  653. Perform pull-specific actions after operation, see
  654. `guix-after-pull-hook' and `guix-update-after-pull'."
  655. (guix-eval-in-repl
  656. (apply #'guix-make-guile-expression 'guix-command args)
  657. nil 'pull))
  658. (defun guix-run-view-build-log (args)
  659. "Add --log-file to ARGS, run 'guix ARGS ...' build command, and
  660. open the log file(s)."
  661. (let* ((args (if (member "--log-file" args)
  662. args
  663. (cl-list* (car args) "--log-file" (cdr args))))
  664. (output (guix-command-output args))
  665. (files (split-string output "\n" t)))
  666. (dolist (file files)
  667. (guix-build-log-find-file file))))
  668. (declare-function guix-make-view-graph "guix-graph" t)
  669. (defun guix-run-view-graph (args)
  670. "Run 'guix ARGS ...' graph command, make the image and open it."
  671. (require 'guix-graph)
  672. (guix-make-view-graph
  673. (if (member "--backend=d3js" args) "d3js" "graphviz")
  674. (lambda (graph-type graph-file)
  675. (guix-eval-read
  676. (cl-case graph-type
  677. (dot (guix-make-guile-expression
  678. 'pipe-guix-output
  679. args (guix-dot-arguments graph-file)))
  680. (html (guix-make-guile-expression
  681. 'guix-output-to-file args graph-file)))))))
  682. (defun guix-run-view-size-map (args)
  683. "Run 'guix ARGS ...' size command, and open the map file."
  684. (let* ((wished-map-file
  685. (cl-some (lambda (arg)
  686. (and (string-match "--map-file=\\(.+\\)" arg)
  687. (match-string 1 arg)))
  688. args))
  689. (map-file (or wished-map-file (guix-png-file-name)))
  690. (args (if wished-map-file
  691. args
  692. (cl-list* (car args)
  693. (concat "--map-file=" map-file)
  694. (cdr args)))))
  695. (guix-command-output args)
  696. (guix-find-file map-file)))
  697. ;;; Generating popups, actions, etc.
  698. (defmacro guix-command-define-popup-action (name &optional commands)
  699. "Define NAME function to generate (if needed) and run popup for COMMANDS."
  700. (declare (indent 1) (debug t))
  701. (let* ((popup-fun (guix-command-symbol `(,@commands "popup")))
  702. (doc (format "Call `%s' (generate it if needed)."
  703. popup-fun)))
  704. `(defun ,name (&optional arg)
  705. ,doc
  706. (interactive "P")
  707. (unless (fboundp ',popup-fun)
  708. (guix-command-generate-popup ',popup-fun ',commands))
  709. (,popup-fun arg))))
  710. (defmacro guix-command-define-execute-action (name executor
  711. &optional commands)
  712. "Define NAME function to execute the current action for guix COMMANDS.
  713. EXECUTOR function is called with the current command line arguments."
  714. (declare (indent 1) (debug t))
  715. (let* ((arguments-fun (guix-command-symbol `(,@commands "arguments")))
  716. (doc (format "Call `%s' with the current popup arguments."
  717. executor)))
  718. `(defun ,name (&rest args)
  719. ,doc
  720. (interactive (,arguments-fun))
  721. (,executor (append ',commands
  722. (guix-command-post-process-args
  723. ',commands args))))))
  724. (defun guix-command-generate-popup-actions (actions &optional commands)
  725. "Generate 'popup' commands from ACTIONS arguments for guix COMMANDS."
  726. (dolist (action actions)
  727. (let ((fun (guix-command-argument-fun action)))
  728. (unless (fboundp fun)
  729. (eval `(guix-command-define-popup-action ,fun
  730. ,(append commands
  731. (list (guix-command-argument-name action)))))))))
  732. (defun guix-command-generate-execute-actions (actions &optional commands)
  733. "Generate 'execute' commands from ACTIONS arguments for guix COMMANDS."
  734. (dolist (action actions)
  735. (let ((fun (guix-command-argument-fun action)))
  736. (unless (fboundp fun)
  737. (eval `(guix-command-define-execute-action ,fun
  738. ,(guix-command-executor
  739. commands (guix-command-argument-name action))
  740. ,commands))))))
  741. (defun guix-command-generate-popup (name &optional commands)
  742. "Define NAME popup with 'guix COMMANDS ...' interface."
  743. (let* ((command (car commands))
  744. (man-page (concat "guix" (and command (concat "-" command))))
  745. (doc (format "Popup window for '%s' command."
  746. (guix-concat-strings (cons "guix" commands)
  747. " ")))
  748. (args (guix-command-arguments commands))
  749. (switches (guix-command-sort-arguments
  750. (guix-command-switches args)))
  751. (options (guix-command-sort-arguments
  752. (guix-command-options args)))
  753. (popup-actions (guix-command-sort-arguments
  754. (guix-command-actions args)))
  755. (execute-actions (unless popup-actions
  756. (guix-command-execute-arguments commands)))
  757. (actions (or popup-actions execute-actions)))
  758. (if popup-actions
  759. (guix-command-generate-popup-actions popup-actions commands)
  760. (guix-command-generate-execute-actions execute-actions commands))
  761. (eval
  762. `(guix-define-popup ,name
  763. ,doc
  764. 'guix-commands
  765. :man-page ,man-page
  766. :switches ',(mapcar #'guix-command-switch->popup-switch switches)
  767. :options ',(mapcar #'guix-command-option->popup-option options)
  768. :actions ',(mapcar #'guix-command-action->popup-action actions)
  769. :max-action-columns 4))))
  770. (declare-function guix-popup "guix-command" t)
  771. ;;;###autoload (autoload 'guix "guix-command" "Popup window for 'guix'." t)
  772. (guix-command-define-popup-action guix)
  773. (declare-function guix-edit "guix-location" t)
  774. (defalias 'guix-edit-action #'guix-edit)
  775. (defvar guix-command-font-lock-keywords
  776. (eval-when-compile
  777. `((,(rx "("
  778. (group "guix-command-define-"
  779. (or "popup-action"
  780. "execute-action"
  781. "argument-improver"))
  782. symbol-end
  783. (zero-or-more blank)
  784. (zero-or-one
  785. (group (one-or-more (or (syntax word) (syntax symbol))))))
  786. (1 font-lock-keyword-face)
  787. (2 font-lock-function-name-face nil t)))))
  788. (font-lock-add-keywords 'emacs-lisp-mode guix-command-font-lock-keywords)
  789. (provide 'guix-command)
  790. ;;; guix-command.el ends here