guix-command.el 35 KB

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