guix-pcomplete.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. ;;; guix-pcomplete.el --- Functions for completing guix commands -*- lexical-binding: t -*-
  2. ;; Copyright © 2015, 2017, 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 completions for "guix" command that may be used in
  18. ;; `shell', `eshell' and wherever `pcomplete' works.
  19. ;;; Code:
  20. (require 'pcomplete)
  21. (require 'pcmpl-unix)
  22. (require 'cl-lib)
  23. (require 'guix nil t)
  24. (require 'guix-read)
  25. (require 'guix-misc)
  26. (require 'guix-utils)
  27. (require 'guix-help-vars)
  28. ;;; Parsing guix output
  29. (defun guix-pcomplete-search-in-help (regexp &optional group
  30. &rest args)
  31. "Search for REGEXP in 'guix ARGS... --help' output.
  32. Return a list of strings matching REGEXP.
  33. GROUP specifies a parenthesized expression used in REGEXP."
  34. (with-temp-buffer
  35. (insert (guix-help-string args))
  36. (let (result)
  37. (guix-while-search regexp
  38. (push (match-string-no-properties group) result))
  39. (nreverse result))))
  40. (defmacro guix-pcomplete-define-options-finder (name docstring regexp
  41. &optional filter)
  42. "Define function NAME to receive guix options and commands.
  43. The defined function takes rest COMMANDS argument. This function
  44. will search for REGEXP in 'guix COMMANDS... --help' output (or
  45. 'guix --help' if COMMANDS is nil) using
  46. `guix-pcomplete-search-in-help' and will return its result.
  47. If FILTER is specified, it should be a function. The result is
  48. passed to this FILTER as argument and the result value of this
  49. function call is returned."
  50. (declare (doc-string 2) (indent 1))
  51. `(guix-memoized-defun ,name (&rest commands)
  52. ,docstring
  53. (let ((res (apply #'guix-pcomplete-search-in-help
  54. ,regexp guix-help-parse-regexp-group commands)))
  55. ,(if filter
  56. `(funcall ,filter res)
  57. 'res))))
  58. (guix-pcomplete-define-options-finder guix-pcomplete-commands
  59. "If COMMANDS is nil, return a list of available guix commands.
  60. If COMMANDS is non-nil (it should be a list of strings), return
  61. available subcommands, actions, etc. for 'guix COMMANDS'."
  62. guix-help-parse-command-regexp)
  63. (guix-pcomplete-define-options-finder guix-pcomplete-long-options
  64. "Return a list of available long options for 'guix COMMANDS'."
  65. guix-help-parse-long-option-regexp)
  66. (guix-pcomplete-define-options-finder guix-pcomplete-short-options
  67. "Return a string with available short options for 'guix COMMANDS'."
  68. guix-help-parse-short-option-regexp
  69. (lambda (list)
  70. (guix-concat-strings list "")))
  71. ;;; Completing
  72. (defvar guix-pcomplete-option-regexp (rx string-start "-")
  73. "Regexp to match an option.")
  74. (defvar guix-pcomplete-long-option-regexp (rx string-start "--")
  75. "Regexp to match a long option.")
  76. (defvar guix-pcomplete-long-option-with-arg-regexp
  77. (rx string-start
  78. (group "--" (one-or-more any)) "="
  79. (group (zero-or-more any)))
  80. "Regexp to match a long option with its argument.
  81. The first parenthesized group defines the option and the second
  82. group - the argument.")
  83. (defvar guix-pcomplete-short-option-with-arg-regexp
  84. (rx string-start
  85. (group "-" (not (any "-")))
  86. (group (zero-or-more any)))
  87. "Regexp to match a short option with its argument.
  88. The first parenthesized group defines the option and the second
  89. group - the argument.")
  90. (defun guix-pcomplete-match-option ()
  91. "Return non-nil, if the current argument is an option."
  92. (pcomplete-match guix-pcomplete-option-regexp 0))
  93. (defun guix-pcomplete-match-long-option ()
  94. "Return non-nil, if the current argument is a long option."
  95. (pcomplete-match guix-pcomplete-long-option-regexp 0))
  96. (defun guix-pcomplete-match-long-option-with-arg ()
  97. "Return non-nil, if the current argument is a long option with value."
  98. (pcomplete-match guix-pcomplete-long-option-with-arg-regexp 0))
  99. (defun guix-pcomplete-match-short-option-with-arg ()
  100. "Return non-nil, if the current argument is a short option with value."
  101. (pcomplete-match guix-pcomplete-short-option-with-arg-regexp 0))
  102. (defun guix-pcomplete-long-option-arg (option args)
  103. "Return a long OPTION's argument from a list of arguments ARGS."
  104. (let* ((re (concat "\\`" option "=\\(.*\\)"))
  105. (args (cl-member-if (lambda (arg)
  106. (string-match re arg))
  107. args))
  108. (cur (car args)))
  109. (when cur
  110. (match-string-no-properties 1 cur))))
  111. (defun guix-pcomplete-short-option-arg (option args)
  112. "Return a short OPTION's argument from a list of arguments ARGS."
  113. (let* ((re (concat "\\`" option "\\(.*\\)"))
  114. (args (cl-member-if (lambda (arg)
  115. (string-match re arg))
  116. args))
  117. (cur (car args)))
  118. (when cur
  119. (let ((arg (match-string-no-properties 1 cur)))
  120. (if (string= "" arg)
  121. (cadr args) ; take the next arg
  122. arg)))))
  123. (defun guix-pcomplete-complete-comma-args (entries)
  124. "Complete comma separated arguments using ENTRIES."
  125. (let ((index pcomplete-index))
  126. (while (= index pcomplete-index)
  127. (let* ((args (if (or (guix-pcomplete-match-long-option-with-arg)
  128. (guix-pcomplete-match-short-option-with-arg))
  129. (pcomplete-match-string 2 0)
  130. (pcomplete-arg 0)))
  131. (input (if (string-match ".*,\\(.*\\)" args)
  132. (match-string-no-properties 1 args)
  133. args)))
  134. (pcomplete-here* entries input)))))
  135. (defun guix-pcomplete-complete-command-arg (command)
  136. "Complete argument for guix COMMAND."
  137. (cond
  138. ((member command
  139. '("archive" "build" "challenge" "copy" "edit" "environment"
  140. "graph" "lint" "pack" "refresh" "size"))
  141. (while t
  142. (pcomplete-here (guix-package-names))))
  143. (t (pcomplete-here* (pcomplete-entries)))))
  144. (defun guix-pcomplete-complete-option-arg (command option &optional input)
  145. "Complete argument for COMMAND's OPTION.
  146. INPUT is the current partially completed string."
  147. (cl-flet ((option? (short long)
  148. (or (string= option short)
  149. (string= option long)))
  150. (command? (&rest commands)
  151. (member command commands))
  152. (complete (entries)
  153. (pcomplete-here entries input nil t))
  154. (complete* (entries)
  155. (pcomplete-here* entries input t)))
  156. (cond
  157. ((option? "-L" "--load-path")
  158. (complete* (pcomplete-dirs)))
  159. ((string= "--key-download" option)
  160. (complete* guix-help-key-policies))
  161. ((and (command? "pack" "package" "weather" "refresh")
  162. (option? "-m" "--manifest"))
  163. (complete* (pcomplete-entries)))
  164. ((command? "package")
  165. (cond
  166. ;; For '--install[=]' and '--remove[=]', try to complete a package
  167. ;; name (INPUT) after the "=" sign, and then the rest packages
  168. ;; separated with spaces.
  169. ((or (option? "-i" "--install")
  170. (option? "-r" "--remove"))
  171. (complete (guix-package-names))
  172. (while (not (guix-pcomplete-match-option))
  173. (pcomplete-here (guix-package-names))))
  174. ((string= "--show" option)
  175. (complete (guix-package-names)))
  176. ((option? "-p" "--profile")
  177. (complete* (pcomplete-dirs)))
  178. ((string= "--search-paths" option)
  179. (complete* guix-help-search-paths-types))
  180. ((or (option? "-f" "--install-from-file")
  181. (option? "-m" "--manifest"))
  182. (complete* (pcomplete-entries)))))
  183. ((and (command? "archive" "build" "environment" "size" "weather")
  184. (option? "-s" "--system"))
  185. (complete* guix-help-system-types))
  186. ((and (command? "archive")
  187. (option? "-x" "--extract"))
  188. (complete* (pcomplete-dirs)))
  189. ((and (command? "build")
  190. (or (option? "-f" "--file")
  191. (option? "-r" "--root")
  192. (string= "--with-source" option)))
  193. (complete* (pcomplete-entries)))
  194. ((command? "graph")
  195. (cond
  196. ((option? "-t" "--type")
  197. (complete* (guix-graph-node-type-names)))
  198. ((option? "-b" "--backend")
  199. (complete* (guix-graph-backend-names)))))
  200. ((and (command? "environment")
  201. (option? "-l" "--load"))
  202. (complete* (pcomplete-entries)))
  203. ((and (command? "hash" "download")
  204. (option? "-f" "--format"))
  205. (complete* guix-help-hash-formats))
  206. ((and (command? "lint")
  207. (option? "-c" "--checkers"))
  208. (guix-pcomplete-complete-comma-args
  209. (guix-lint-checker-names)))
  210. ((command? "pack")
  211. (cond
  212. ((option? "-C" "--compression")
  213. (complete* (guix-compressor-names)))
  214. ((option? "-f" "--format")
  215. (complete* (guix-pack-format-names)))
  216. ;; Although the argument should be "FILE-NAME=TARGET", it is
  217. ;; still better to complete the FILE-NAME than to complete
  218. ;; nothing.
  219. ((option? "-S" "--symlink")
  220. (complete* (pcomplete-entries)))))
  221. ((command? "potluck")
  222. (cond
  223. ;; ((option? "--license")) ; TODO
  224. ((member option '("--scratch" "--source" "--target"))
  225. (complete* (pcomplete-dirs)))))
  226. ((command? "publish")
  227. (cond
  228. ((member option '("--public-key" "--private-key"))
  229. (complete* (pcomplete-entries)))
  230. ((option? "-u" "--user")
  231. (complete* (pcmpl-unix-user-names)))))
  232. ((command? "refresh")
  233. (cond
  234. ((option? "-s" "--select")
  235. (complete* guix-help-refresh-subsets))
  236. ((option? "-t" "--type")
  237. (guix-pcomplete-complete-comma-args
  238. (guix-refresh-updater-names)))))
  239. ((command? "size")
  240. (cond
  241. ((option? "-m" "--map-file")
  242. (complete* (pcomplete-entries)))
  243. ((string= "--sort" option)
  244. (complete* guix-help-size-sort-keys)))))))
  245. (defun guix-pcomplete-complete-options (command)
  246. "Complete options (with their arguments) for guix COMMAND."
  247. (while (guix-pcomplete-match-option)
  248. (let ((index pcomplete-index))
  249. (if (guix-pcomplete-match-long-option)
  250. ;; Long options.
  251. (if (guix-pcomplete-match-long-option-with-arg)
  252. (let ((option (pcomplete-match-string 1 0))
  253. (arg (pcomplete-match-string 2 0)))
  254. (guix-pcomplete-complete-option-arg
  255. command option arg))
  256. (pcomplete-here* (guix-pcomplete-long-options command))
  257. ;; We support '--opt arg' style (along with '--opt=arg'),
  258. ;; because 'guix package --install/--remove' may be used this
  259. ;; way. So try to complete an argument after the option has
  260. ;; been completed.
  261. ;;
  262. ;; XXX This leads to a problem: optional arguments cannot be
  263. ;; completed. For example, after typing "guix build --sources ",
  264. ;; most likely, a user would want to complete a package name, so
  265. ;; we can't complete sources type there.
  266. (unless (guix-pcomplete-match-option)
  267. (guix-pcomplete-complete-option-arg
  268. command (pcomplete-arg 0 -1))))
  269. ;; Short options.
  270. (let ((arg (pcomplete-arg 0)))
  271. (if (> (length arg) 2)
  272. ;; Support specifying an argument after a short option without
  273. ;; spaces (for example, '-L/tmp/foo').
  274. (guix-pcomplete-complete-option-arg
  275. command
  276. (substring-no-properties arg 0 2)
  277. (substring-no-properties arg 2))
  278. (pcomplete-opt (guix-pcomplete-short-options command))
  279. (guix-pcomplete-complete-option-arg
  280. command (pcomplete-arg 0 -1)))))
  281. ;; If there were no completions, move to the next argument and get
  282. ;; out if the last argument is achieved.
  283. (when (= index pcomplete-index)
  284. (if (= pcomplete-index pcomplete-last)
  285. (throw 'pcompleted nil)
  286. (pcomplete-next-arg))))))
  287. ;;;###autoload
  288. (defun pcomplete/guix ()
  289. "Completion for `guix'."
  290. (let ((commands (guix-pcomplete-commands)))
  291. (pcomplete-here* (cons "--help" commands))
  292. (let ((command (pcomplete-arg 'first 1)))
  293. (when (member command commands)
  294. (guix-pcomplete-complete-options command)
  295. (let ((subcommands (guix-pcomplete-commands command)))
  296. (when subcommands
  297. (pcomplete-here* subcommands)))
  298. (guix-pcomplete-complete-options command)
  299. (guix-pcomplete-complete-command-arg command)))))
  300. (provide 'guix-pcomplete)
  301. ;;; guix-pcomplete.el ends here