summaryrefslogtreecommitdiff
path: root/lisp/pcmpl-linux.el
diff options
context:
space:
mode:
authorAugusto Stoffel <arstoffel@gmail.com>2022-09-08 11:09:42 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2022-09-14 21:58:04 +0200
commita9941269683fe50673d0aa81feefb7a9d3d8a6b9 (patch)
tree566c9ecd3afb90b58607c71ad794cee14ba7b823 /lisp/pcmpl-linux.el
parent05971d2b8d47e69e9585d0d6066b8a607555aa48 (diff)
downloademacs-a9941269683fe50673d0aa81feefb7a9d3d8a6b9.tar.gz
pcomplete: Generate completions from --help messages
* lisp/pcomplete.el (pcomplete-from-help): New function (and hash table) to get pcomplete candidates from help messages. (pcomplete-here-using-help): Helper function to define pcomplete for simple commands (pcomplete-completions-at-point): Provide annotation-function and company-docsig properties. * lisp/pcmpl-git.el: New file, provides pcomplete for Git. * lisp/pcmpl-gnu.el: Add pcomplete for awk, gpg and gdb, emacs and emacsclient. * lisp/pcmpl-linux.el: Add pcomplete for systemctl and journalctl. * lisp/pcmpl-rpm.el: Add pcomplete for dnf. * lisp/pcmpl-unix.el: Add pcomplete for sudo and most commands found in GNU Coreutils. * lisp/pcmpl-x.el: Add pcomplete for tex, pdftex, latex, pdflatex, rigrep and rclone. * test/lisp/pcomplete-tests.el (pcomplete-test-parse-gpg-help, pcomplete-test-parse-git-help): Tests for the new functions.
Diffstat (limited to 'lisp/pcmpl-linux.el')
-rw-r--r--lisp/pcmpl-linux.el68
1 files changed, 68 insertions, 0 deletions
diff --git a/lisp/pcmpl-linux.el b/lisp/pcmpl-linux.el
index 7c072f3d40c..023c655a2a8 100644
--- a/lisp/pcmpl-linux.el
+++ b/lisp/pcmpl-linux.el
@@ -30,6 +30,7 @@
(provide 'pcmpl-linux)
(require 'pcomplete)
+(eval-when-compile (require 'rx))
;; Functions:
@@ -111,4 +112,71 @@ Test is done using `equal'."
(pcomplete-uniquify-list points)
(cons "swap" (pcmpl-linux-mounted-directories))))))
+;;; systemd
+
+(defun pcmpl-linux--systemd-units (&rest args)
+ "Run `systemd list-units ARGS' and return the output as a list."
+ (with-temp-buffer
+ (apply #'call-process
+ "systemctl" nil '(t nil) nil
+ "list-units" "--full" "--legend=no" "--plain" args)
+ (goto-char (point-min))
+ (let (result)
+ (while (re-search-forward (rx bol (group (+ (not space)))
+ (+ space) (+ (not space))
+ (+ space) (group (+ (not space)))
+ (+ space) (+ (not space))
+ (+ space) (group (* nonl)))
+ nil t)
+ (push (match-string 1) result)
+ (put-text-property 0 1 'pcomplete-annotation
+ (concat " " (match-string 2))
+ (car result))
+ (put-text-property 0 1 'pcomplete-description
+ (match-string 3)
+ (car result)))
+ (nreverse result))))
+
+;;;###autoload
+(defun pcomplete/systemctl ()
+ "Completion for the `systemctl' command."
+ (let ((subcmds (pcomplete-from-help
+ "systemctl --help"
+ :margin (rx bol " " (group) alpha)
+ :argument (rx (+ (any alpha ?-)))
+ :metavar (rx (group (+ " " (>= 2 (any upper "[]|."))))))))
+ (while (not (member (pcomplete-arg 1) subcmds))
+ (if (string-prefix-p "-" (pcomplete-arg 0))
+ (pcomplete-here (pcomplete-from-help "systemctl --help"
+ :metavar "[^ ]+"
+ :separator " \\(\\)-"))
+ (pcomplete-here subcmds)))
+ (let ((subcmd (pcomplete-arg 1))
+ (context (if (member "--user" pcomplete-args) "--user" "--system")))
+ (while (pcase subcmd
+ ((guard (string-prefix-p "-" (pcomplete-arg 0)))
+ (pcomplete-here
+ (pcomplete-from-help "systemctl --help")))
+ ;; TODO: suggest only relevant units to each subcommand
+ ("start"
+ (pcomplete-here
+ (pcmpl-linux--systemd-units context "--state" "inactive,failed")))
+ ((or "restart" "stop")
+ (pcomplete-here
+ (pcmpl-linux--systemd-units context "--state" "active")))
+ (_ (pcomplete-here
+ (completion-table-in-turn
+ (pcmpl-linux--systemd-units context "--all")
+ (pcomplete-entries)))))))))
+
+;;;###autoload
+(defun pcomplete/journalctl ()
+ "Completion for the `journalctl' command."
+ (while (if (string-prefix-p "-" (pcomplete-arg 0))
+ (pcomplete-here (pcomplete-from-help "journalctl --help"
+ :metavar "[^ ]+"
+ :separator " \\(\\)-"))
+ (pcomplete-here (mapcar (lambda (s) (concat s "="))
+ (process-lines "journalctl" "--fields"))))))
+
;;; pcmpl-linux.el ends here