summaryrefslogtreecommitdiff
path: root/eix.el
blob: 649676d6af7fad7971a13d27b094f9cb62e3b41f (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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
;;; eix.el --- Eix integration -*- lexical-binding: t -*-


;; Copyright 2022 Gentoo Authors


;; This file 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 2 of the License, or
;; (at your option) any later version.

;; This file 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.


;; Author: Maciej Barć <xgqt@riseup.net>
;; Homepage: https://gitweb.gentoo.org/proj/emacs-eix.git
;; Keywords: processes
;; Maintainer: <emacs@gentoo.org>
;; Package-Requires: ((emacs "24.3"))
;; Version: 0.0.0



;;; Commentary:


;; Eix integration for Emacs.
;; Interface with the "eix" Gentoo tool from within GNU Emacs.

;; Eix resources:
;; - repository: https://github.com/vaeth/eix
;; - Gentoo Wiki page: https://wiki.gentoo.org/wiki/Eix


;; Potential improvement / alternative:
;; Eix provides output in XML format with the "--xml" switch.
;; Maybe this way we can customize the output more or get more info.


;; This library was originally written as part of "emacs-gentoo"
;; by Maciej Barć.
;; It was later relicensed by the author under the GPL-2-or-later license
;; and republished under the Gentoo GNU Emacs project.

;; Original repository: https://gitlab.com/xgqt/emacs-gentoo



;;; Code:


(require 'shell)


(defconst eix-version "0.0.0"
  "Emacs-Eix version.")

(defgroup eix nil
  "Eix integration."
  :group 'external)


;; Executables

(defcustom eix-command (executable-find "eix")
  "Path to the \"eix\" binary."
  :safe 'stringp
  :type 'file
  :group 'eix)

(defcustom eix-diff-command (concat eix-command "-diff")
  "Path to the \"eix-diff\" binary."
  :safe 'stringp
  :type 'file
  :group 'eix)

(defcustom eix-update-command (concat eix-command "-update")
  "Path to the \"eix-update\" binary."
  :safe 'stringp
  :type 'file
  :group 'eix)

(defcustom eix-remote-command (concat eix-command "-remote")
  "Path to the \"eix-remote\" binary."
  :safe 'stringp
  :type 'file
  :group 'eix)

(defcustom eix-sync-command (concat eix-command "-sync")
  "Path to the \"eix-sync\" binary."
  :safe 'stringp
  :type 'file
  :group 'eix)

(defcustom eix-test-command (concat eix-command "-test-obsolete")
  "Path to the \"eix-test-obsolete\" binary."
  :safe 'stringp
  :type 'file
  :group 'eix)


;; Helpers

(defun eix--execute-command (&rest args)
  "Execute a command constructed of ARGS in the EIX buffer."
  (let ((eix-buffer-name "*EIX*")
        (eix-error-buffer-name "*EIX ERRORS*")
        (cmd (apply 'concat (mapcar (lambda (s) (concat " " s)) args)))
        (process-environment (append process-environment '("EIX_LIMIT=0"))))
    (let ((eix-buffer (get-buffer-create eix-buffer-name)))
      (with-current-buffer eix-buffer
        (async-shell-command cmd eix-buffer-name eix-error-buffer-name)
        (eix-browse-mode)))))

;; TODO: local only?

(defun eix--get-all-local-packages ()
  "Gel all available packages in local repositories.
Used primarily for `completing-read' in `eix-search-exact'"
  (let ((process-environment (append process-environment '("EIX_LIMIT=0"))))
    (split-string
     (shell-command-to-string (concat eix-command " --only-names")) "\n" t)))


;; Mode

(defcustom eix-browse-mode-hook nil
  "Hook for `eix-browse' major mode."
  :type 'hook
  :group 'eix)

(defconst eix-browse-font-lock-keywords
  '(("->" . 'font-lock-keyword-face)  ; arrow
    (" [!<=>]+" . 'font-lock-variable-name-face)  ; !>=cat/pkg
    (" [+-][0-9a-z-]+" . 'font-lock-constant-face)  ; +z3 -qt5
    ("xpak" . 'font-lock-comment-face))
  "Font-lock keywords for `eix-browse' major mode.")

(defvar eix-browse-mode-map
  (let ((eix-browse-mode-map (make-keymap)))
    (define-key eix-browse-mode-map (kbd "/") 'isearch-forward)
    (define-key eix-browse-mode-map (kbd "?") 'describe-mode)
    (define-key eix-browse-mode-map (kbd "h") 'describe-mode)
    (define-key eix-browse-mode-map (kbd "q") 'quit-window)
    (define-key eix-browse-mode-map (kbd "r") 'isearch-backward)
    (define-key eix-browse-mode-map (kbd "s") 'isearch-forward)
    eix-browse-mode-map)
  "Key map for `eix-browse' major mode.")

(define-derived-mode eix-browse-mode shell-mode "eix-browse"
  "Major mode for browsing \"eix\" command's output.
Do not use anywhere else."
  (run-hooks 'eix-browse-mode-hook)
  (use-local-map eix-browse-mode-map)
  (setq font-lock-defaults '(eix-browse-font-lock-keywords))
  (goto-address-mode)
  (setq buffer-read-only t))


;; Main provided features

;; TODO: add call options? e.g.: -R, -O, ...

;;;###autoload
(defun eix-diff ()
  "Show eix cache differences."
  (interactive)
  (eix--execute-command eix-diff-command))

;;;###autoload
(defun eix-local-update ()
  "Update eix database of local packages."
  (interactive)
  (eix--execute-command eix-update-command))

;;;###autoload
(defun eix-remote-update ()
  "Update eix database of remote packages."
  (interactive)
  (eix--execute-command eix-remote-command "update"))

;; TODO: (eix-search) completing-read of --verbose --in-overlay ?

;;;###autoload
(defun eix-search (&optional package)
  "Search for a PACKAGE."
  (interactive)
  (let ((pkg (if package package (read-string "Enter package name: "))))
    (eix--execute-command eix-command "--verbose" pkg)))

;;;###autoload
(defun eix-search-exact ()
  "Search for a package using completion of available packages."
  (interactive)
  (let ((package (completing-read "Package: " (eix--get-all-local-packages))))
    (eix-search package)))

;;;###autoload
(defun eix-sync ()
  "Synchronize package repositories."
  (interactive)
  (eix--execute-command eix-sync-command))

;;;###autoload
(defun eix-test ()
  "Test for obsolete packages."
  (interactive)
  (eix--execute-command eix-test-command))

;;;###autoload
(defun eix-show-updates ()
  "List packages that can be updated."
  (interactive)
  (eix--execute-command eix-command "--compact" "--upgrade"))


(provide 'eix)



;;; eix.el ends here