Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

el-get-list-packages.el 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
;;; el-get --- Manage the external elisp bits and pieces you depend upon
;;
;; Copyright (C) 2010-2011 Dimitri Fontaine
;;
;; Author: Dimitri Fontaine <dim@tapoueh.org>
;; URL: http://www.emacswiki.org/emacs/el-get
;; GIT: https://github.com/dimitri/el-get
;; Licence: WTFPL, grab your copy here: http://sam.zoy.org/wtfpl/
;;
;; This file is NOT part of GNU Emacs.
;;
;; Install
13
;;     Please see the README.md file from the same distribution
14 15 16 17 18

;;
;;
;; Description of packages.  (Code based on `describe-function').
;;
19 20 21
(require 'el-get-core)
(require 'cl)

22 23 24 25 26 27 28 29 30 31 32
(define-button-type 'el-get-help-package-def
  :supertype 'help-xref
  'help-function (lambda (package) (find-file (el-get-recipe-filename package)))
  'help-echo (purecopy "mouse-2, RET: find package's recipe"))

(define-button-type 'el-get-help-install
  :supertype 'help-xref
  'help-function (lambda (package)
                   (when (y-or-n-p
                          (format "Do you really want to install `%s'? "
                                  package))
Ryan C. Thompson's avatar
Ryan C. Thompson committed
33
                     (el-get-install package)))
34 35 36 37 38 39
  'help-echo (purecopy "mouse-2, RET: install package"))

(define-button-type 'el-get-help-remove
  :supertype 'help-xref
  'help-function (lambda (package)
                   (when (y-or-n-p
Ryan C. Thompson's avatar
Ryan C. Thompson committed
40 41 42
                          (format "Do you really want to uninstall `%s'? "
                                  package))
                     (el-get-remove package)))
43 44 45 46 47 48
  'help-echo (purecopy "mouse-2, RET: remove package"))

(define-button-type 'el-get-help-update
  :supertype 'help-xref
  'help-function (lambda (package)
                   (when (y-or-n-p
Ryan C. Thompson's avatar
Ryan C. Thompson committed
49 50 51
                          (format "Do you really want to update `%s'? "
                                  package))
                     (el-get-update package)))
52 53
  'help-echo (purecopy "mouse-2, RET: update package"))

54 55 56 57 58
(define-button-type 'el-get-help-cd
  :supertype 'help-xref
  'help-function #'dired
  'help-echo (purecopy "mouse-2, RET: open directory"))

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
(define-button-type 'el-get-help-describe-package
  :supertype 'help-xref
  'help-function #'el-get-describe
  'help-echo (purecopy "mouse-2, RET: describe package"))

(defun el-get-describe-princ-button (label regex type &rest args)
  "Princ a new button with label LABEL.

The LABEL is made clickable by calling `help-xref-button' for a backwards
matching REGEX with TYPE and ARGS as parameter."
  (princ label)
  (with-current-buffer standard-output
    (save-excursion
      (re-search-backward regex nil t)
      (apply #'help-xref-button 1 type args))))

75 76 77 78 79
(defun el-get-guess-website (package)
  (let* ((type (el-get-package-type package))
         (guesser (el-get-method type :guess-website)))
    (when guesser
      (funcall guesser package))))
80

81 82 83 84 85 86 87
(defun el-get-describe-1 (package)
  (let* ((psym (el-get-as-symbol package))
         (pname (symbol-name psym))
         (status (el-get-read-package-status package))
         (def (el-get-package-def pname))
         (name (plist-get def :name))
         (website (plist-get def :website))
88
         (directory (el-get-package-directory package))
89
         (descr (plist-get def :description))
90 91
         (type (el-get-package-method def))
         (builtin (plist-get def :builtin))
92
         (minimum-version (plist-get def :minimum-emacs-version))
93 94
         (url (plist-get def :url))
         (depends (plist-get def :depends)))
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    (princ (format "%s is an `el-get' package.  " name))
    (if (eq type 'builtin)
        (princ (format "It is built-in since Emacs %s" builtin))
      (princ (format "It is currently %s "
                     (if status
                         status
                       "not installed")))
      (cond
       ((string= status "installed")
        (el-get-describe-princ-button "[update]" "\\[\\([^]]+\\)\\]"
                                      'el-get-help-update package)
        (el-get-describe-princ-button "[remove]" "\\[\\([^]]+\\)\\]"
                                      'el-get-help-remove package))
       ((string= status "required")
        (el-get-describe-princ-button "[update]" "\\[\\([^]]+\\)\\]"
                                      'el-get-help-update package))
       (t
        (el-get-describe-princ-button "[install]" "\\[\\([^]]+\\)\\]"
                                      'el-get-help-install package))))
114 115
    (princ ".\n\n")

116
    (let ((website (or website
117
                       (el-get-guess-website package))))
118 119 120
      (when website
        (el-get-describe-princ-button (format "Website: %s\n" website)
                                      ": \\(.+\\)" 'help-url website)))
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    (when descr
      (princ (format "Description: %s\n" descr)))
    (when depends
      (if (listp depends)
          (progn
            (princ "Dependencies: ")
            (loop for i in depends
                  do (el-get-describe-princ-button
                      (format "`%s'" i) "`\\([^`']+\\)"
                      'el-get-help-describe-package i)))
        (princ "Dependency: ")
        (el-get-describe-princ-button
         (format "`%s'" depends) "`\\([^`']+\\)"
         'el-get-help-describe-package depends))
      (princ ".\n"))
136 137 138 139 140 141
    (when minimum-version
      (princ (format "Requires minimum Emacs version: %s." minimum-version))
      (when (version-list-< (version-to-list emacs-version)
                            (el-get-version-to-list minimum-version))
        (princ (format "  Warning: Your Emacs is too old (%s)!" emacs-version)))
      (princ "\n"))
142
    (if (eq type 'builtin)
143 144
        (princ (format "The package is built-in since Emacs %s.\n" builtin))
      (princ (format "The default installation method is %s%s.\n" type
145
                     (if url (format " from %s" url) ""))))
146 147 148 149 150 151
    (when (string= status "installed")
      (princ "Installed in ")
      (el-get-describe-princ-button (format "`%s'" directory) "`\\([^']+\\)"
                                    'el-get-help-cd directory)
      (princ ".\n"))
    (princ "\n")
152 153 154 155 156 157 158
    (princ "Full definition")
    (let ((file (el-get-recipe-filename package)))
      (if (not file)
          (princ ":\n")
        (el-get-describe-princ-button (format " in `%s':\n" file)
                                      "`\\([^`']+\\)"
                                      'el-get-help-package-def package)))
159
    (princ (el-get-print-to-string def))))
160 161 162 163 164 165 166 167 168 169

(defun el-get-describe (package)
  "Generate a description for PACKAGE."
  (interactive
   (list
    (el-get-read-package-name "Describe")))

  (if (null package)
      (message "You didn't specify a package")
    (help-setup-xref (list #'el-get-describe package)
Ryan C. Thompson's avatar
Ryan C. Thompson committed
170
                     (called-interactively-p 'interactive))
171 172 173 174 175 176
    (save-excursion
      (with-help-window (help-buffer)
        (el-get-describe-1 package)
        (with-current-buffer standard-output
          (buffer-string))))))

177 178 179 180 181 182 183
(defcustom el-get-package-menu-view-recipe-function
  'find-file-other-window
  "`find-file' compatible function used to display recipe content
in el-get package menu."
  :group 'el-get
  :type 'symbol)

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

;;
;; Package Menu
;;
(defvar el-get-package-menu-mode-hook nil
  "Hooks to run after el-get package menu init.")

(defvar el-get-package-menu-mode-map nil
  "Keymap for el-get-package-menu-mode")

(defvar el-get-package-menu-sort-key nil
  "sort packages by key")

(defun el-get-package-menu-get-package-name ()
  (save-excursion
    (beginning-of-line)
    (if (looking-at ". \\([^ \t]*\\)")
Ryan C. Thompson's avatar
Ryan C. Thompson committed
201
        (match-string 1))))
202

203 204 205 206 207 208 209 210
(defun el-get-package-menu-view-recipe ()
  "Show package recipe in a read-only mode."
  (interactive)
  (let* ((package (el-get-package-menu-get-package-name))
         (recipe-file (el-get-recipe-filename package)))
    (funcall el-get-package-menu-view-recipe-function recipe-file)
    (view-mode)))

211 212 213 214
(defun el-get-package-menu-get-status ()
  (save-excursion
    (beginning-of-line)
    (if (looking-at ". [^ \t]*[ \t]*\\([^ \t\n]*\\)")
Ryan C. Thompson's avatar
Ryan C. Thompson committed
215
        (match-string 1))))
216 217 218 219 220 221 222 223

(defun el-get-package-menu-mark (what)
  (unless (eobp)
    (let ((buffer-read-only nil))
      (beginning-of-line)
      (delete-char 1)
      (insert what)
      (forward-line)
Ryan C. Thompson's avatar
Ryan C. Thompson committed
224
      (setq buffer-read-only t))))
225 226 227 228

(defun el-get-package-menu-mark-install ()
  (interactive)
  (if (or (string= (el-get-package-menu-get-status) "available")
Ryan C. Thompson's avatar
Ryan C. Thompson committed
229 230
          (string= (el-get-package-menu-get-status) "removed"))
      (el-get-package-menu-mark "I")))
231 232 233 234

(defun el-get-package-menu-mark-update ()
  (interactive)
  (if (or (string= (el-get-package-menu-get-status) "installed")
Ryan C. Thompson's avatar
Ryan C. Thompson committed
235 236
          (string= (el-get-package-menu-get-status) "required"))
      (el-get-package-menu-mark "U")))
237 238 239 240

(defun el-get-package-menu-mark-delete ()
  (interactive)
  (if (or (string= (el-get-package-menu-get-status) "installed")
Ryan C. Thompson's avatar
Ryan C. Thompson committed
241 242
          (string= (el-get-package-menu-get-status) "required"))
      (el-get-package-menu-mark "D")))
243 244 245 246 247 248 249 250

(defun el-get-package-menu-mark-unmark ()
  (interactive)
  (el-get-package-menu-mark " "))

(defun el-get-package-menu-revert ()
  (interactive)
  (let ((current-point (point)))
Ryan C. Thompson's avatar
Ryan C. Thompson committed
251 252 253
    (el-get-package-menu)
    (goto-char current-point)
    (beginning-of-line)))
254 255 256 257

(defun el-get-package-menu-execute ()
  (interactive)
  (let ((current-point (point)))
Ryan C. Thompson's avatar
Ryan C. Thompson committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    (goto-char (point-min))
    (while (not (eobp))
      (let ((command (char-after))
            (package-name (el-get-package-menu-get-package-name)))
        (cond
         ((eq command ?I)
          (message "Installing %s..." package-name)
          (el-get-install package-name)
          (message "Installing %s...done" package-name))
         ((eq command ?U)
          (message "Updating %s..." package-name)
          (el-get-update package-name)
          (message "Updating %s...done" package-name))
         ((eq command ?D)
          (message "Deleting %s..." package-name)
          (el-get-remove package-name)
          (message "Deleting %s..." package-name))))
      (forward-line))
    (el-get-package-menu-revert)
    (goto-char current-point)
    (beginning-of-line)))
279 280 281 282 283 284 285

(defun el-get-package-menu-describe ()
  (interactive)
  (el-get-describe (el-get-package-menu-get-package-name)))

(defun el-get-package-menu-quick-help ()
  (interactive)
286
  (message "n-ext, p-revious, i-nstall, u-pdate, d-elete, SPC-unmark, g-revert, x-execute, ?-package describe, v-iew recipe, h-elp, q-uit"))
287 288 289 290 291 292 293 294 295 296 297 298 299

(unless el-get-package-menu-mode-map
  (setq el-get-package-menu-mode-map (make-keymap))
  (suppress-keymap el-get-package-menu-mode-map)
  (define-key el-get-package-menu-mode-map "n" 'next-line)
  (define-key el-get-package-menu-mode-map "p" 'previous-line)
  (define-key el-get-package-menu-mode-map "i" 'el-get-package-menu-mark-install)
  (define-key el-get-package-menu-mode-map "u" 'el-get-package-menu-mark-update)
  (define-key el-get-package-menu-mode-map "d" 'el-get-package-menu-mark-delete)
  (define-key el-get-package-menu-mode-map " " 'el-get-package-menu-mark-unmark)
  (define-key el-get-package-menu-mode-map "g" 'el-get-package-menu-revert)
  (define-key el-get-package-menu-mode-map "x" 'el-get-package-menu-execute)
  (define-key el-get-package-menu-mode-map "?" 'el-get-package-menu-describe)
300
  (define-key el-get-package-menu-mode-map "v" 'el-get-package-menu-view-recipe)
301 302 303 304
  (define-key el-get-package-menu-mode-map "h" 'el-get-package-menu-quick-help)
  (define-key el-get-package-menu-mode-map "q" 'quit-window))

(defun el-get-package-menu-mode ()
305 306 307
  "Major mode for browsing a list of packages.

\\{el-get-package-menu-mode-map}"
308 309 310 311 312 313 314
  (kill-all-local-variables)
  (use-local-map el-get-package-menu-mode-map)
  (setq major-mode 'el-get-package-menu-mode)
  (setq mode-name "Package-Menu")
  (setq buffer-read-only t)
  (setq truncate-lines t)
  (if (fboundp 'run-mode-hooks)
Ryan C. Thompson's avatar
Ryan C. Thompson committed
315 316
      (run-mode-hooks 'el-get-package-menu-mode-hook)
    (run-hooks 'el-get-package-menu-mode-hook)))
317 318 319

(defun el-get-print-package (package-name status desc)
  (let ((face
Ryan C. Thompson's avatar
Ryan C. Thompson committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
         (cond
          ((string= status "installed")
           'font-lock-comment-face)
          ((string= status "required")
           'font-lock-keyword-face)
          ((string= status "removed")
           'font-lock-string-face)
          (t
           (setq status "available")
           'default))))
    (indent-to 2 1)
    (insert (propertize package-name 'font-lock-face face))
    (indent-to 30 1)
    (insert (propertize status 'font-lock-face face))
    (when desc
      (indent-to 41 1)
      (insert (propertize
               (replace-regexp-in-string "\n" " " desc)
               'font-lock-face face)))
    (insert "\n")))
340 341 342

(defun el-get-list-all-packages ()
  (with-current-buffer (get-buffer-create "*el-get packages*")
Ryan C. Thompson's avatar
Ryan C. Thompson committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    (setq buffer-read-only nil)
    (erase-buffer)
    (let ((packages (el-get-read-all-recipes)))
      (let ((selector (cond
                       ((string= el-get-package-menu-sort-key "Status")
                        #'(lambda (package)
                            (let ((package-name (el-get-as-string (plist-get package :name))))
                              (el-get-read-package-status package-name))))
                       ((string= el-get-package-menu-sort-key "Description")
                        #'(lambda (package)
                            (plist-get package :description)))
                       (t
                        #'(lambda (package)
                            (el-get-as-string (plist-get package :name)))))))
        (setq packages
              (sort packages
                    (lambda (left right)
                      (let ((vleft (funcall selector left))
                            (vright (funcall selector right)))
                        (string< vleft vright))))))
      (mapc (lambda (package)
              (let ((package-name (el-get-as-string (plist-get package :name))))
                (el-get-print-package package-name
                                      (el-get-read-package-status package-name)
                                      (plist-get package :description))))
            packages))
    (goto-char (point-min))
    (current-buffer)))
371 372 373 374 375 376

(defun el-get-package-menu-sort-by-column (&optional e)
  "Sort the package menu by the last column clicked on."
  (interactive (list last-input-event))
  (if e (mouse-select-window e))
  (let* ((pos (event-start e))
Ryan C. Thompson's avatar
Ryan C. Thompson committed
377 378 379 380
         (obj (posn-object pos))
         (col (if obj
                  (get-text-property (cdr obj) 'column-name (car obj))
                (get-text-property (posn-point pos) 'column-name))))
381
    (setq el-get-package-menu-sort-key col)
Ryan C. Thompson's avatar
Ryan C. Thompson committed
382
    (el-get-package-menu)))
383 384 385 386 387 388 389 390 391 392

(defvar el-get-package-menu-sort-button-map
  (let ((map (make-sparse-keymap)))
    (define-key map [header-line mouse-1] 'el-get-package-menu-sort-by-column)
    (define-key map [follow-link] 'mouse-face)
    map)
  "Local keymap for package menu sort buttons.")

(defun el-get-package-menu ()
  (with-current-buffer (el-get-list-all-packages)
Ryan C. Thompson's avatar
Ryan C. Thompson committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    (el-get-package-menu-mode)
    (setq header-line-format
          (mapconcat
           (lambda (pair)
             (let ((column (car pair))
                   (name (cdr pair)))
               (concat
                ;; Insert a space that aligns the button properly.
                (propertize " " 'display (list 'space :align-to column)
                            'face 'fixed-pitch)
                ;; Set up the column button.
                (propertize name
                            'column-name name
                            'help-echo "mouse-1: sort by column"
                            'mouse-face 'highlight
                            'keymap el-get-package-menu-sort-button-map))))
           '((2 . "Package")
             (30 . "Status")
             (41 . "Description"))
           ""))
    (pop-to-buffer (current-buffer))))
414

415
;;;###autoload
416 417 418 419 420 421
(defun el-get-list-packages ()
  "Display a list of packages."
  (interactive)
  (el-get-package-menu))

(provide 'el-get-list-packages)