-
Notifications
You must be signed in to change notification settings - Fork 11
/
helm-system-packages-dnf.el
183 lines (156 loc) · 7.18 KB
/
helm-system-packages-dnf.el
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
;;; helm-system-packages-dnf.el --- Helm UI for RPM-based distros using DNF. -*- lexical-binding: t -*-
;; Copyright (C) 2018 Damien Cassou <[email protected]>
;; Author: Damien Cassou <[email protected]>
;; Maintainer: Pierre Neidhardt <[email protected]>
;; URL: https://github.com/emacs-helm/helm-system-packages
;; Version: 1.10.2
;; Package-Requires: ((emacs "24.4") (helm "2.8.6"))
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Helm UI for RPM-based distributions using DNF.
;;; Code:
(require 'helm-system-packages)
(require 'seq)
(defcustom helm-system-packages-dnf-actions
(list
(cons "Show package(s)" #'helm-system-packages-dnf-info)
(cons "Install" #'helm-system-packages-dnf-install)
(cons "Uninstall" #'helm-system-packages-dnf-uninstall)
(cons "Browse homepage URL" #'helm-system-packages-dnf-browse-url)
(cons "Find files" #'helm-system-packages-dnf-find-files))
"Actions for Helm DNF."
:group 'helm-system-packages
:type '(alist :key-type string :value-type function))
(defun helm-system-packages-dnf--package-attributes-to-org-description-list ()
"Convert \"Key : value\" to \"- Key :: value\".
This is org format for description list items."
(goto-char (point-min))
(while (re-search-forward "^\\(\\<[^ ]*\\) +: \\(.*\\)$" nil t)
(let ((key (match-string-no-properties 1))
(value (match-string-no-properties 2)))
(replace-match (format "- %s :: %s" key value) nil t))))
(defun helm-system-packages-dnf--info (packages)
"Return a list of (NAME . DESC) describing PACKAGES."
(save-match-data
(with-temp-buffer
(apply #'process-file "dnf" nil t nil "info" packages)
(goto-char (point-min))
(helm-system-packages-dnf--merge-descriptions)
(helm-system-packages-dnf--package-attributes-to-org-description-list)
(goto-char (point-min))
(cl-loop
while (re-search-forward "^- Name :: \\(.*\\)$" nil t)
collect (cons (match-string-no-properties 1)
(buffer-substring-no-properties
(match-beginning 0)
(save-match-data
(re-search-forward "^$")
(match-beginning 0))))))))
(defun helm-system-packages-dnf-info (candidate)
"Print information about helm CANDIDATE.
With prefix argument, insert the output at point.
Otherwise display in `helm-system-packages-buffer'."
(helm-system-packages-show-information
`((uninstalled . ,(helm-system-packages-dnf--info
(if helm-in-persistent-action
(list candidate)
(helm-marked-candidates)))))))
(defun helm-system-packages-dnf-install (_)
"Install marked candidates."
(helm-system-packages-run-as-root "dnf" "install"))
(defun helm-system-packages-dnf-uninstall (_)
"Uninstall marked candidates."
(helm-system-packages-run-as-root "dnf" "remove"))
(defun helm-system-packages-dnf-browse-url (_)
"Print homepage URLs of `helm-marked-candidates'.
With prefix argument, insert the output at point.
Otherwise display in `helm-system-packages-buffer'."
(helm-system-packages-browse-url
(save-match-data
(with-temp-buffer
(apply #'process-file "dnf" nil t nil "info" (helm-marked-candidates))
(goto-char (point-min))
(cl-loop
while (re-search-forward "^URL *: \\(.*\\)$" nil t)
collect (match-string-no-properties 1) into urls
finally return (seq-uniq urls #'string=))))))
(defun helm-system-packages-dnf--list-files (package)
"Return a list of all files installed by PACKAGE."
(message "Collecting files of %s" package)
(save-match-data
(with-temp-buffer
(process-file "dnf" nil t nil "repoquery" "-l" package)
(goto-char (point-min))
(cl-loop
while (re-search-forward "^\\(/.*\\)$" nil t)
collect (match-string-no-properties 1)))))
(defun helm-system-packages-dnf-find-files (_)
"Find files for marked candidates."
(let* ((package-files (make-hash-table :test #'equal)))
(dolist (package (helm-marked-candidates))
(puthash package (helm-system-packages-dnf--list-files package) package-files))
(helm-system-packages-find-files package-files)))
(defun helm-system-packages-dnf--delete-non-package-lines ()
"Remove every line of current package that is not a package."
;; delete summary line:
(goto-char (point-min))
(delete-region (point-min) (line-beginning-position 2)))
(defun helm-system-packages-dnf--merge-descriptions ()
"Merge description of packages spanning several lines in current buffer."
(goto-char (point-min))
(while (re-search-forward "^ *\\(...\\|\\):" nil t)
(delete-region (line-end-position 0) (point))))
(defun helm-system-packages-dnf--format-packages ()
"Format each package and description in current buffer.
In particular, descriptions are vertically aligned."
(goto-char (point-min))
(save-match-data
(while (re-search-forward "^\\([^ ]*\\) : \\(.*\\)$" nil t)
(let ((package-name (match-string 1))
(package-description (match-string 2)))
(replace-match
(format (concat "%-" (number-to-string helm-system-packages-column-width) "s %s")
package-name package-description)
nil t)))))
(defun helm-system-packages-dnf--sort-by-package-name ()
"Sort packages in current buffer by package name."
(sort-lines nil (point-min) (point-max)))
(defun helm-system-packages-dnf--list-packages ()
"List all packages in current buffer with descriptions."
(process-file "dnf" nil '(t nil) nil "search" "--quiet" "*")
(helm-system-packages-dnf--delete-non-package-lines)
(helm-system-packages-dnf--merge-descriptions)
(helm-system-packages-dnf--format-packages)
(helm-system-packages-dnf--sort-by-package-name))
(defun helm-system-packages-dnf--remove-descriptions ()
"Remove all package descriptions in current buffer.
Only package names remain."
(goto-char (point-min))
(while (re-search-forward "^[^ ]+" nil t)
(delete-region (match-end 0) (line-end-position))))
(defun helm-system-packages-dnf-refresh ()
"Refresh the package list."
(interactive)
(save-match-data
(with-temp-buffer
(helm-system-packages-dnf--list-packages)
(let ((descriptions (buffer-string)))
(helm-system-packages-dnf--remove-descriptions)
(helm-system-packages--cache-set (buffer-string) descriptions nil "dnf")))))
(defvar helm-system-packages-dnf
(helm-system-packages-manager-create
:name "dnf"
:refresh-function #'helm-system-packages-dnf-refresh
:dependencies '("dnf")
:actions helm-system-packages-dnf-actions))
(provide 'helm-system-packages-dnf)
;;; helm-system-packages-dnf.el ends here