summaryrefslogtreecommitdiff
path: root/pcmpl-emerge.el
blob: ae28191a7bc72e05db77fef0b5dcc3e462292626 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
;;; pcmpl-rc.el --- Completions For Emerge Command  -*- lexical-binding: t; -*-

;; Copyright (C) 2024  Thanos Apollo

;; Author: Thanos Apollo <[email protected]>
;; Keywords: pcomplete completions emerge gentoo
;; URL: https://git.thanosapollo.com/pcmpl-emerge
;; Version: 0.0.1

;; Package-Requires: ((emacs "27.2"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Under development

;;; Code:

(require 'cl-lib)
(require 'pcomplete)
(require 'subr-x)

(defvar pcmpl-emerge-package-completions '()
  "Cache for emerge package completions.")

(defvar pcmpl-emerge-options '("--sync" "--info" "--help" "-h" "--depclean" "-c"
			       "--check-news" "--clean" "-av" "-avuDN"
			       "--config" "--deselect" "-W" "--prune" "-P" "--regen"
			       "--resume" "-r" "--searchdesc" "-S" "--unmerge" "-C"
			       "--version" "-V" "--accept-properties=" "--accept-restrict="
			       "--alert" "-A" "--alphabetical" "--ask" "-a" "--autounmask-backtrack"
			       "--autounmask" "--autounmask-only" "--autounmask-continue"
			       "--autounmask-unrestricted-atoms" "--autounmask-keep-keywords"
			       "--autounmask-keep-masks" "--autounmask-license" "--autounmask-use"
			       "--autounmask-write" "--binpkg-changed-deps" "--binpkg-respect-use"
			       "--buildpkg" "-b" "--buildpkg-exclude" "--buildpkgonly" "-B")
  "Emerge options.")


(defun pcmpl-emerge-list-packages-in-repo (repo)
  "Return a list of package paths in REPO."
  (let (result)
    (setf result (split-string (shell-command-to-string
				(format "find %s -mindepth 2 -maxdepth 2 -type d -not -path '*/\.*'" repo))
			       "\n" t))
    (setf result (mapcar
		  (lambda (str) (string-remove-prefix (concat "/var/db/repos/" (file-name-nondirectory repo) "/") str))
		  result))
    result))

(defun pcmpl-emerge-update-completions ()
  "Update the completions cache."
  (let ((top-dir "/var/db/repos/")
	paths)
    (cl-loop for repo in (seq-filter (lambda (f) (not (string-match-p "\\.git$" f)))
                                     (directory-files top-dir t directory-files-no-dot-files-regexp))
             do (setf paths (append paths (seq-filter
                                            (lambda (str)
                                              (not (string-match-p "/\\.git/" str)))
                                            (pcmpl-emerge-list-packages-in-repo repo)))))
    (setf pcmpl-emerge-package-completions (seq-uniq paths))))

;;;###autoload
(defun pcomplete/emerge ()
  "Completion rules for the `emerge' command."
  (unless pcmpl-emerge-package-completions
    (pcmpl-emerge-update-completions))
  (while (pcomplete-arg)
    (if (string-prefix-p "-" (pcomplete-arg))
	(pcomplete-here* pcmpl-emerge-options)
      (pcomplete-here* pcmpl-emerge-package-completions))))

(provide 'pcmpl-emerge)