guix-pcomplete.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. ;;; guix-pcomplete.el --- Functions for completing 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 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" "edit" "environment"
  140. "graph" "lint" "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. ((command? "package")
  162. (cond
  163. ;; For '--install[=]' and '--remove[=]', try to complete a package
  164. ;; name (INPUT) after the "=" sign, and then the rest packages
  165. ;; separated with spaces.
  166. ((or (option? "-i" "--install")
  167. (option? "-r" "--remove"))
  168. (complete (guix-package-names))
  169. (while (not (guix-pcomplete-match-option))
  170. (pcomplete-here (guix-package-names))))
  171. ((string= "--show" option)
  172. (complete (guix-package-names)))
  173. ((option? "-p" "--profile")
  174. (complete* (pcomplete-dirs)))
  175. ((or (option? "-f" "--install-from-file")
  176. (option? "-m" "--manifest"))
  177. (complete* (pcomplete-entries)))))
  178. ((and (command? "archive" "build" "environment" "size")
  179. (option? "-s" "--system"))
  180. (complete* guix-help-system-types))
  181. ((and (command? "archive")
  182. (option? "-x" "--extract"))
  183. (complete* (pcomplete-dirs)))
  184. ((and (command? "build")
  185. (or (option? "-f" "--file")
  186. (option? "-r" "--root")
  187. (string= "--with-source" option)))
  188. (complete* (pcomplete-entries)))
  189. ((command? "graph")
  190. (cond
  191. ((option? "-t" "--type")
  192. (complete* (guix-graph-node-type-names)))
  193. ((option? "-b" "--backend")
  194. (complete* (guix-graph-backend-names)))))
  195. ((and (command? "environment")
  196. (option? "-l" "--load"))
  197. (complete* (pcomplete-entries)))
  198. ((and (command? "hash" "download")
  199. (option? "-f" "--format"))
  200. (complete* guix-help-hash-formats))
  201. ((and (command? "lint")
  202. (option? "-c" "--checkers"))
  203. (guix-pcomplete-complete-comma-args
  204. (guix-lint-checker-names)))
  205. ((and (command? "publish")
  206. (option? "-u" "--user"))
  207. (complete* (pcmpl-unix-user-names)))
  208. ((command? "refresh")
  209. (cond
  210. ((option? "-s" "--select")
  211. (complete* guix-help-refresh-subsets))
  212. ((option? "-t" "--type")
  213. (guix-pcomplete-complete-comma-args
  214. (guix-refresh-updater-names)))))
  215. ((and (command? "size")
  216. (option? "-m" "--map-file"))
  217. (complete* (pcomplete-entries))))))
  218. (defun guix-pcomplete-complete-options (command)
  219. "Complete options (with their arguments) for guix COMMAND."
  220. (while (guix-pcomplete-match-option)
  221. (let ((index pcomplete-index))
  222. (if (guix-pcomplete-match-long-option)
  223. ;; Long options.
  224. (if (guix-pcomplete-match-long-option-with-arg)
  225. (let ((option (pcomplete-match-string 1 0))
  226. (arg (pcomplete-match-string 2 0)))
  227. (guix-pcomplete-complete-option-arg
  228. command option arg))
  229. (pcomplete-here* (guix-pcomplete-long-options command))
  230. ;; We support '--opt arg' style (along with '--opt=arg'),
  231. ;; because 'guix package --install/--remove' may be used this
  232. ;; way. So try to complete an argument after the option has
  233. ;; been completed.
  234. ;;
  235. ;; XXX This leads to a problem: optional arguments cannot be
  236. ;; completed. For example, after typing "guix build --sources ",
  237. ;; most likely, a user would want to complete a package name, so
  238. ;; we can't complete sources type there.
  239. (unless (guix-pcomplete-match-option)
  240. (guix-pcomplete-complete-option-arg
  241. command (pcomplete-arg 0 -1))))
  242. ;; Short options.
  243. (let ((arg (pcomplete-arg 0)))
  244. (if (> (length arg) 2)
  245. ;; Support specifying an argument after a short option without
  246. ;; spaces (for example, '-L/tmp/foo').
  247. (guix-pcomplete-complete-option-arg
  248. command
  249. (substring-no-properties arg 0 2)
  250. (substring-no-properties arg 2))
  251. (pcomplete-opt (guix-pcomplete-short-options command))
  252. (guix-pcomplete-complete-option-arg
  253. command (pcomplete-arg 0 -1)))))
  254. ;; If there were no completions, move to the next argument and get
  255. ;; out if the last argument is achieved.
  256. (when (= index pcomplete-index)
  257. (if (= pcomplete-index pcomplete-last)
  258. (throw 'pcompleted nil)
  259. (pcomplete-next-arg))))))
  260. ;;;###autoload
  261. (defun pcomplete/guix ()
  262. "Completion for `guix'."
  263. (let ((commands (guix-pcomplete-commands)))
  264. (pcomplete-here* (cons "--help" commands))
  265. (let ((command (pcomplete-arg 'first 1)))
  266. (when (member command commands)
  267. (guix-pcomplete-complete-options command)
  268. (let ((subcommands (guix-pcomplete-commands command)))
  269. (when subcommands
  270. (pcomplete-here* subcommands)))
  271. (guix-pcomplete-complete-options command)
  272. (guix-pcomplete-complete-command-arg command)))))
  273. (provide 'guix-pcomplete)
  274. ;;; guix-pcomplete.el ends here