guix-command.el 33 KB

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