guix-command.el 38 KB

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